GridView- Difference between asp:hyperlinkfield and asp:hyperlink

<asp:gridview id=”titlesGrid” runat=”server”
      datasourceid=”titles”
      width=90% cellpadding=5 font-size=”8pt”
      autogeneratecolumns=false
      headerstyle-backcolor=”maroon”
      headerstyle-forecolor=”khaki”
      headerstyle-font-bold
      rowstyle-verticalalign=”top”>

      <columns>
         <asp:hyperlinkfieldheadertext=”Title”
            datatextfield=”title”
            datanavigateurlformatstring=”page_details.aspx?id={0}”
            datanavigateurlfields=”id” />

          <asp:boundfield headertext=”ID”
            datafield=”id” />

         <asp:boundfield headertext=”Name”
            datafield=”Name” />

         <asp:boundfield headertext=”Fee”
            datafield=”fee”
            htmlencode=false
            dataformatstring=”{0:n2}”
            itemstyle-horizontalalign=”right” />

      </columns>

   </asp:gridview>

The only drawback I found in a asp:hyperlinkfieldthat you cannot bound the field or concatenate the string like below expression. You will get an error. 

<asp:hyperlinkfieldheadertext=”Title”
            datatextfield=”title”
            datanavigateurlformatstring=”page_details.aspx?id={0}
&status=<%= Status %>”  datanavigateurlfields=”id” />

Below is a suggested way to get it done.
Create an template column and add a hyperlink to it.

<asp:TemplateField HeaderText=”Link”>
<ItemTemplate>
<asp:HyperLink ID=”hltitle” runat=”server” NavigateUrl='<%# FormatUrl( (int) Eval(“title”) ) %>’ Text = ‘<%# Eval(“title”) %>‘ />

</ItemTemplate>
</asp:TemplateField>

Most recommendable way to do it, do it through code behind. use RowDataBound events.

Sub GridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

If
e.Row.RowType = DataControlRowType.DataRow Then
‘You can bound your data here 
End If

I would appreciate your comments.
Rana
 

Advertisements

5 thoughts on “GridView- Difference between asp:hyperlinkfield and asp:hyperlink

  1. I have a question: How can I build a hyperlink with 2 values that I get from a request.querystring method and 1 value from the same row? thank you

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