How to pass Gridview Parameters to another page using HyperLinkField
In this article I'm trying to explain how to pass parameters from Gridview Control to another page. There is an inbuilt Template called as HyperLinkField using that Template and using DataNavigateUrlFields and DataNavigateUrlFormatString< properties we can achieve our goal easily.
How to pass Gridview Parameters to another page:
In this article I'm trying to explain how to pass parameters from Gridview Control to another page. There is an inbuilt Template called as HyperLinkField using that Template and using DataNavigateUrlFields and DataNavigateUrlFormatString properties we can achieve our goal easily.Description:
Everyone face some problem while transfer Grdiview controls to another page, most of the people is using TemplateField and inside of that they are using LinkButton to transfer controls to another page. But there are no.of ways to transfer Gridview Controls to another page. The best and easiest way to transfer Grid controls to another page by using HyperLinkField template of Gridview. This is Gridview inbuilt Template using this template we can easily Transfer Gridview Parameters to another page. There are 2 properties name as DataNavigateUrlFields and DataNavigateUrlFormatString using those two properties we can easily transfer Gridview Parameters.
DataNavigateUrlFields - Using this we can pass parameter value
DataNavigateUrlFormatString - Using this we can give navigated page URL.Sample Code:
<asp:GridView ID="gv" runat="server" Width="60%" CssClass="ArialNarrow" OnRowEditing="gv_RowEditing" OnRowCreated="gv_RowCreated" AutoGenerateColumns="false" BorderWidth="1px" BorderColor="Black" BorderStyle="Solid">
<SelectedRowStyle BackColor="White" ForeColor="White" />
<AlternatingRowStyle BackColor="AliceBlue" HorizontalAlign="left" />
<RowStyle BackColor="White" ForeColor="Black" HorizontalAlign="left" />
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" BackColor="#000084" Font-Bold="True" ForeColor="White" />
<columns>
<asp:BoundField DataField="Short_Desc" HeaderText="Short Name" HeaderStyle-ForeColor="White" />
<asp:TemplateField HeaderText="Name" HeaderStyle-ForeColor="White">
<ItemTemplate>
<asp:Label ID="lblDesc" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Desc") %>' />
<asp:Label ID="lblId" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Id") %>' Visible="false" />
<asp:Label ID="lblActive_Tag" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Active_Tag") %>' Visible="false" />
<asp:Label ID="lblCode" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Code") %>' Visible="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Active_Tag_Desc" HeaderText="Active Status" HeaderStyle-ForeColor="White"/>
<asp:HyperLinkField Text="View" DataNavigateUrlFields="Id,Desc,Short_Desc" DataNavigateUrlFormatString="~/Page2.aspx?CL_Id={0}&CL_Desc={1}&CL_Short_Desc={2}" />
</columns>
</asp:GridView>
Now using HyperLinkField template we can able to transfer values from page1 to page2. Now, in page2 we can able to call those Parameters using QueryString.
Refer below sample in page2.aspx
String Id="",Desc="",Short_Desc="";
Protected void Page_Load(object sender, EventArgs e)
{
If( Request.QueryString["Id"] != null)
Id = Request.QueryString["Id"].ToString();
If( Request.QueryString["Desc"] != null)
Desc = Request.QueryString["Desc"].ToString();
If( Request.QueryString["Short_Desc"] != null)
Short_Desc = Request.QueryString["Short_Desc"].ToString();
}Conclusion:
Using above code you can easily transfer Gridview controls from one page to another page