HyperLink inside a GridView

Using HyperLinkField of GridView

<ASP:GRIDVIEW id=GridView1 runat="server" autogeneratecolumns="False" datakeynames="CustomerID" datasourceid="SqlDataSource1">     
  <COLUMNS>     
        <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>     
</COLUMNS> 
</ASP:GRIDVIEW> 
<ASP:SQLDATASOURCE id=SqlDataSource1 runat="server" connectionstring="<%$ ConnectionStrings:NORTHWNDConnectionString %>" 

Passing more than one Querystring parameter with HyperLinkField in GridView:
<asp:hyperlinkfield text="Detail" datanavigateurlfields="CustomerID,CompanyName" 
datanavigateurlformatstring="CustomerDetails.aspx?customerId={0}&companyName={1}" />

Set NavigateUrl property of HyperLink in TemplateField in Markup:
<asp:TemplateField>
       <ItemTemplate> 
                <asp:HyperLink ID="hlDetails1" Text="Details" runat="server"  NavigateUrl='<%# "CustomerDetails.aspx?customer=" + Eval("CustomerID") + "&CompanyName=" + Server.UrlEncode(Eval("CompanyName").ToString())%>' /> 
</ItemTemplate> 
    </asp:TemplateField>
Set NavigateUrl in RowDataBound event:
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()); } }}
Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: