Thursday 9 February 2012

Pass Send GridView Row Value/Data Using Hyperlink in ASP.NET


Pass Send GridView Row Value/Data Using Hyperlink in ASP.NET

In this example i am going to describe how to pass transfer or send GridView data or values from GridView row to Other asp.net page using hyperlink in GridView.

I've put a hyperlink column in gridview to pass values through querystring, and using request.querystring on the second page to retrieve values.








We need to set DataNavigateUrlFields and DataNavigateUrlFormatString properties of hyperlink in gridview to pass the row data 

HTML markup of the page look like
<asp:GridView ID="GridView1" runat="server" 
              AutoGenerateColumns="False" 
              DataSourceID="SqlDataSource1">
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="ID,Name,Location" 
DataNavigateUrlFormatString=
"Default2.aspx?id={0}&name={1}&loc={2}" 
Text="Transfer values to other page" />
<asp:BoundField DataField="ID" HeaderText="ID" 
                SortExpression="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" 
                SortExpression="Name" />
<asp:BoundField DataField="Location" HeaderText="Location" 
                SortExpression="Location" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ID], [Name], [Location] FROM [Details]">
</asp:SqlDataSource>

Now write code mentioned below to retrieve values on Default2.aspx page
C# code behind
1protected void Page_Load(object sender, EventArgs e)
2    {
3        string strID = Request.QueryString["id"];
4        string strName = Request.QueryString["name"];
5        string strLocation = Request.QueryString["loc"];
6        lblID.Text = strID;
7        lblName.Text = strName;
8        lblLocation.Text = strLocation;
9    }

No comments:

Post a Comment