Home > ASP.Net > Pass QueryString Parameter with NavigaterUrl in Hyperlink inside a GridView

Pass QueryString Parameter with NavigaterUrl in Hyperlink inside a GridView

Hi all,

try this example to try many ways to Pass QueryString Parameter with NavigaterUrl in Hyperlink inside a GridView

1-a: Using HyperLinkField of GridView :-

<ASP:GRIDVIEW id=GridView1 runat="server" autogeneratecolumns="False" datakeynames="CustomerID" datasourceid="SqlDataSource1">
 <ASP:HYPERLINKFIELD text="Detail" datanavigateurlfields="CustomerID" datanavigateurlformatstring="CustomerDetails.aspx?customerId={0}"></ASP:HYPERLINKFIELD>
 <ASP:BOUNDFIELD datafield="CustomerID" headertext="CustomerID" readonly="True" sortexpression="CustomerID"></ASP:BOUNDFIELD>
 <ASP:BOUNDFIELD datafield="CompanyName" headertext="CompanyName" sortexpression="CompanyName"></ASP:BOUNDFIELD>
 <ASP:BOUNDFIELD datafield="ContactName" headertext="ContactName" sortexpression="ContactName"></ASP:BOUNDFIELD>

<ASP:SQLDATASOURCE id=SqlDataSource1 runat="server" connectionstring="<%$ ConnectionStrings:NORTHWNDConnectionString %>"
selectcommand="SELECT [CustomerID], [CompanyName], [ContactName] FROM [Customers]">

1-b: Passing more than one Querystring parameter with HyperLinkField in GridView :-

<asp:hyperlinkfield text="Detail" datanavigateurlfields="CustomerID,CompanyName"
 datanavigateurlformatstring="CustomerDetails.aspx?customerId={0}&amp;companyName={1}" />

2-a: Set NavigateUrl property of HyperLink in TemplateField in Markup :-

 <asp:HyperLink ID="hlDetails1" Text="Details" runat="server"
 NavigateUrl='<%# "CustomerDetails.aspx?customer=" + Eval("CustomerID") + "&CompanyName=" + Server.UrlEncode(Eval("CompanyName").ToString())%>' />

2-b: Set NavigateUrl property of HyperLink by Calling method in code-behind:-

 <asp:HyperLink id="hlDetails2" Text="Details" Runat="server"
 NavigateUrl='<%# GetUrl(Eval("CustomerID"),Eval("CompanyName"))%>' />

and in code behind add this method

public string GetUrl(object id, object companyname)
 string url = "~/CustomerDetails.aspx?customerid=" + id.ToString() + "&companyname=" +
 return url;

2-c: Set NavigateUrl in RowDataBound event :-

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID"
DataSourceID="SqlDataSource1" onrowdatabound="GridView1_RowDataBound">
 <asp:HyperLink id="hlDetails2" Text="Details" Runat="server" />

and in code behind :-

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
 if (e.Row.RowType == DataControlRowType.DataRow)
 HyperLink hl = (HyperLink)e.Row.FindControl("hlDetails2");
 if (hl != null)
 DataRowView drv = (DataRowView)e.Row.DataItem;
 string id = drv["CustomerID"].ToString();
 string companyname = drv["CompanyName"].ToString();
 hl.NavigateUrl = "~/CustomerDetails.aspx?customerid=" + id.ToString() + "&companyname=" + Server.UrlEncode(companyname.ToString());

Hope this helps

Good Luck

Categories: ASP.Net

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: