如何将 SqlDataSource 查询结果(在 aspx 文件中指定)与 ButtonField 一起使用?
How to use result of SqlDataSource query (specified in aspx file) with ButtonField?
我在 SqlDataSource
(.aspx
文件)中指定了一个 SelectCommand
。此 SelectCommand
查询将 return 数据显示在 GridView
中。
现在在select命令中有一个属性名为id
(主键),其数据不会显示在GridView
上,我想发送当单击相应的 ButtonField
时,此 'id' 转到另一个页面。
上图为GridView
。每列代表 activity 个字段,而每一行代表一个唯一的 activity。最后一列有 ButtonFields
用于注册 activity。
我想将学生发送到 Register For Activity 页面以及单击行的 'id'。请注意,'id' 的值未显示在 GridView
上。
请给我一个实现它的好方法。
我认为一种解决方案是:在后端 (.cs) 文件中访问 'id' 的值,然后通过 ButtonField 命令发送 id 的值。但我不确定是否可以在 .cs 文件中访问它?
ASPX代码:
<asp:GridView ID="ShowActivitiesGridView" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="name" HeaderText="Name" SortExpression="name" />
<asp:BoundField DataField="location" HeaderText="Location" SortExpression="location" />
<asp:BoundField DataField="date_of_activity" HeaderText="Date of Activity" SortExpression="date_of_activity" />
<asp:BoundField DataField="organization" HeaderText="Organization" SortExpression="organization" />
<asp:BoundField DataField="no_of_student" HeaderText="No of Student" SortExpression="no_of_student" />
<asp:BoundField DataField="description" HeaderText="Description" SortExpression="description" />
<asp:ButtonField CommandName="RowCommand" HeaderText="Register For Activity" ShowHeader="True" Text="Register" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT [id], [name], [location], [date_of_activity], [organization], [no_of_student], [description] FROM [Activities]">
</asp:SqlDataSource>
为 aspx
上的 ItemTemplate 替换 ButtonField
<asp:GridView ID="ShowActivitiesGridView" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="name" HeaderText="Name" SortExpression="name" />
<asp:BoundField DataField="location" HeaderText="Location" SortExpression="location" />
<asp:BoundField DataField="date_of_activity" HeaderText="Date of Activity" SortExpression="date_of_activity" />
<asp:BoundField DataField="organization" HeaderText="Organization" SortExpression="organization" />
<asp:BoundField DataField="no_of_student" HeaderText="No of Student" SortExpression="no_of_student" />
<asp:BoundField DataField="description" HeaderText="Description" SortExpression="description" />
<asp:ButtonField CommandName="RowCommand" HeaderText="Register For Activity" ShowHeader="True" Text="Register" />
<asp:TemplateField HeaderText="Remove" ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="btnRegisterActivity" runat="server" CausesValidation="false" Text="Register For Activity" CommandArgument='<%# Eval("id") %>' oncommand="btnRegisterActivity_Command" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT [id], [name], [location], [date_of_activity], [organization], [no_of_student], [description] FROM [Activities]">
</asp:SqlDataSource>
页面cs
文件
protected void btnRegisterActivity_Command(object sender, CommandEventArgs e)
{
int myId = (int)e.CommandArgument;
//Do whatever you want
}
我在 SqlDataSource
(.aspx
文件)中指定了一个 SelectCommand
。此 SelectCommand
查询将 return 数据显示在 GridView
中。
现在在select命令中有一个属性名为id
(主键),其数据不会显示在GridView
上,我想发送当单击相应的 ButtonField
时,此 'id' 转到另一个页面。
上图为GridView
。每列代表 activity 个字段,而每一行代表一个唯一的 activity。最后一列有 ButtonFields
用于注册 activity。
我想将学生发送到 Register For Activity 页面以及单击行的 'id'。请注意,'id' 的值未显示在 GridView
上。
请给我一个实现它的好方法。
我认为一种解决方案是:在后端 (.cs) 文件中访问 'id' 的值,然后通过 ButtonField 命令发送 id 的值。但我不确定是否可以在 .cs 文件中访问它?
ASPX代码:
<asp:GridView ID="ShowActivitiesGridView" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="name" HeaderText="Name" SortExpression="name" />
<asp:BoundField DataField="location" HeaderText="Location" SortExpression="location" />
<asp:BoundField DataField="date_of_activity" HeaderText="Date of Activity" SortExpression="date_of_activity" />
<asp:BoundField DataField="organization" HeaderText="Organization" SortExpression="organization" />
<asp:BoundField DataField="no_of_student" HeaderText="No of Student" SortExpression="no_of_student" />
<asp:BoundField DataField="description" HeaderText="Description" SortExpression="description" />
<asp:ButtonField CommandName="RowCommand" HeaderText="Register For Activity" ShowHeader="True" Text="Register" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT [id], [name], [location], [date_of_activity], [organization], [no_of_student], [description] FROM [Activities]">
</asp:SqlDataSource>
为 aspx
<asp:GridView ID="ShowActivitiesGridView" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="name" HeaderText="Name" SortExpression="name" />
<asp:BoundField DataField="location" HeaderText="Location" SortExpression="location" />
<asp:BoundField DataField="date_of_activity" HeaderText="Date of Activity" SortExpression="date_of_activity" />
<asp:BoundField DataField="organization" HeaderText="Organization" SortExpression="organization" />
<asp:BoundField DataField="no_of_student" HeaderText="No of Student" SortExpression="no_of_student" />
<asp:BoundField DataField="description" HeaderText="Description" SortExpression="description" />
<asp:ButtonField CommandName="RowCommand" HeaderText="Register For Activity" ShowHeader="True" Text="Register" />
<asp:TemplateField HeaderText="Remove" ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="btnRegisterActivity" runat="server" CausesValidation="false" Text="Register For Activity" CommandArgument='<%# Eval("id") %>' oncommand="btnRegisterActivity_Command" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT [id], [name], [location], [date_of_activity], [organization], [no_of_student], [description] FROM [Activities]">
</asp:SqlDataSource>
页面cs
文件
protected void btnRegisterActivity_Command(object sender, CommandEventArgs e)
{
int myId = (int)e.CommandArgument;
//Do whatever you want
}