Passing Value when Select on GridView to another page
I have a GridView which is showing some data:
Entity_ID (PK)
Name Description
Now I am enabling Select in my GridView. I need to pass Entity_ID to another page and in this page I am showing more contents for this Entity_ID.
How should I pick the Entity_ID value and pass it in as Quer开发者_开发知识库y String? I have this code:
ProductsDataGridView.SelectedRows(0).Cells(1).Value.ToString()
Any responses will be appreciated! Thank you.
Add a new item template column in you grid and add the select link as below.
<asp:TemplateField HeaderText="View Details">
<ItemTemplate>
<asp:HyperLink ID="lnkSelect" runat='server' NavigateUrl='<%# String.Format("~/detailspagename.aspx?ID={0}", Eval("Entity_ID")) %>'>Select</asp:HyperLink>
</ItemTemplate>
This is what I did:
protected void gvAgentList_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = gvAgentList.SelectedRow;
Response.Redirect("~/FrontEnd/Registration.aspx? EntityID=" + row.Cells[0].Text);
}
Use the OnRowSelected event. Once it calls that you can get the selected row and then the entity id. Next you can build a string with the entity id in the query string and response.redirect to that page.
You can also use DataKeys
set DataKeys='Entity_ID'
In the code behind you can access the same as selectedrow.DataKeys[rowindex]["Entity_ID"]
here selected row is the one you selected , rowindex the index and you get the corresponding Entity_ID
@GSGuy:
<asp:GridView runat ="server" ID = "gvAgentList"
AllowPaging = "True"
AutoGenerateSelectButton="True" AllowSorting="True" BackColor="#E8E8E8"
BorderColor="#003399" BorderStyle="Solid" BorderWidth="1px" Height="375px"
Width="823px" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="SqlDataSource1" onselectedindexchanged="gvAgentList_SelectedIndexChanged">
<AlternatingRowStyle ForeColor="#0066CC" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:TemplateField HeaderText="View Details">
<ItemTemplate>
<asp:HyperLink ID="lnkSelect" runat='server' NavigateUrl='<%# String.Format("~/detailspagename.aspx?ID={0}", Eval("Entity_ID")) %>'>Select</asp:HyperLink>
</ItemTemplate>
</Columns>
<HeaderStyle ForeColor="#3366FF" />
</asp:GridView>
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
string fname, lname;
fname = GridView1.Rows[e.NewEditIndex].Cells[0].Text;
Session["fname"] = fname;
lname = GridView1.Rows[e.NewEditIndex].Cells[1].Text;
Session["lname"] = lname;
Response.Redirect("gridpass.aspx");
}
On gridpass.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = Session["fname"].ToString();
TextBox2.Text = Session["lname"].ToString();
}
there are several approaches how to pass data between pages:
http://msdn.microsoft.com/en-us/library/6c3yckfw.aspx
the query string is ok if you don't mind the url will contain the ID
you can also consider Page.PreviousPage from the options above, which seems reasonable in your case
精彩评论