2

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 Query String? I have this code:

ProductsDataGridView.SelectedRows(0).Cells(1).Value.ToString()

Any responses will be appreciated! Thank you.

7 Answers 7

6

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>

7
  • Its giving me err: This name contains Uppercase Char which is not allowed.
    – RG-3
    Mar 22, 2011 at 16:09
  • @klm9971: How you are using this in your page ?
    – MUS
    Mar 22, 2011 at 16:12
  • Is it called this: <asp:TemplateField> </asp:TemplateField>
    – RG-3
    Mar 22, 2011 at 16:14
  • @GSGuy: This snippet I am putting it inside <Columns> here </Columns>.
    – RG-3
    Mar 22, 2011 at 16:15
  • I have updated the answer for more clarity. Put this tag within columns tag of Gridview.
    – MUS
    Mar 22, 2011 at 16:18
2

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);
    }
1

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.

2
  • There is no event called OnRowSelected. Tried that.
    – RG-3
    Mar 22, 2011 at 16:31
  • @klm9971 Yeah, I meant SelectedIndexChanged. OnRowSelected is only available in some third party controls. My bad.
    – m4tt1mus
    Mar 22, 2011 at 16:41
0

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

0

@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>
0
 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();
}
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.