I have specified a SelectCommand
in SqlDataSource
(.aspx
file). This SelectCommand
query will return data, that will be shown in GridView
.
Now there is one property named id
(Primary Key) in select command, whose data will NOT be shown on GridView
, rather I want to send this 'id' to another page when the respective ButtonField
is clicked.
The image above shows GridView
. Every column represents activity fields while every row represents a unique activity. The last column has ButtonFields
for registering for an activity.
I want to send students to Register For Activity Page along with 'id' of the row clicked. Mind you, value of 'id' is not shown on GridView
.
Kindly suggest me a good way to achieve it.
I think one solution could be: Accessing the value of 'id' in backend (.cs) file and then sending id's value through ButtonField Command. but I am not sure if it can be accessed in .cs file?
ASPX code:
<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>
Replace ButtonField for ItemTemplate on the 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>
on the page cs
file
protected void btnRegisterActivity_Command(object sender, CommandEventArgs e)
{
int myId = (int)e.CommandArgument;
//Do whatever you want
}