Fetching the id from the first column of GridView
I have a Grid in which I am showing some records in each row. Here's how it is:
Now, my problem is that when I press the view button, I want to fetch the ID from the first column in a session variable so that I c开发者_如何学编程an display the same ID on the next page.
For the ItemTemplate of EditButton, I am using this code:
<ItemTemplate>
<asp:LinkButton ID="EditBtn" CssClass="btn green" CommandName="edit" ToolTip="Edit" Text="Edit" runat="server" />
</ItemTemplate>
You could try passing it as command argument:
<ItemTemplate>
<asp:LinkButton
ID="EditBtn"
CssClass="btn green"
CommandName="edit"
CommandArgument='<%# Eval("FirstColumnId") %>'
OnCommand="EditCommand"
ToolTip="Edit"
Text="Edit"
runat="server" />
</ItemTemplate>
and in the code behind:
protected void EditCommand(object sender, GridViewCommandEventArgs e)
{
var id = e.CommandArgument;
// TODO: do something with the id
}
Try this. No changes are required at your aspx
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
Session["UserID"] = ((Label)GridView1.Rows[e.NewEditIndex].FindControl("lb1")).Text.Trim();
}
Other method (Using DataKeyNames - Preferred)
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
Session["UserID"] = GridView1.DataKeys[e.NewEditIndex].Value.ToString();
}
精彩评论