title: Adding link dynamically to Template Field
body: <p>I need to replace the link for each data bound item. I already checked out the databound event of the datarow. The problem with that is I can't control where the link shows up. I'm trying to stay away from javascript and keep it mostly in the code behind. Below is my asp code and the code behind I have. I also want to try to keep as much the same as possible, not changing to the html template items. The process Im using is as follows. </p>
  
  <p>I have a custom object which contains all the fields and data of a blog entry. Those items are put into a list and then supplied to the datagrid. </p>
  
  <pre><code>   &lt;asp:GridView ID="grdRecentNews" runat="server" AutoGenerateColumns="False" DataKeyNames="Key"
                              GridLines="None" PageSize="4" ShowHeader="False"
                              AllowPaging="True"&gt;
                              &lt;PagerSettings Visible="False" /&gt;
                              &lt;Columns&gt;
                                  &lt;asp:TemplateField&gt;
                                      &lt;ItemTemplate&gt;
                                          &lt;blockquote&gt;
                                             &lt;%-- &lt;strong&gt;&lt;a href=**"News.aspx"**&gt;--%&gt;
                                              &lt;strong&gt;&lt;a href="News.aspx"&gt;
                                                  &lt;%#Eval("NewsDesc")%&gt;&lt;/a&gt;&lt;/strong&gt;
                                              &lt;br /&gt;
                                              &lt;span class="italic"&gt;
                                                  &lt;%#Eval("NewsDate")%&gt;&lt;/span&gt;
                                          &lt;/blockquote&gt;
                                      &lt;/ItemTemplate&gt;
                                  &lt;/asp:TemplateField&gt;
                              &lt;/Columns&gt;
                          &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>This is the code I had in the Databound event, problem being I only want to provide a link for one of the fields of the Rss object. as above below. I want to replace News.aspx" for each item added to the datagrid with the rssobject's link property.</p>
  
  <pre><code>  Protected Sub grdRecentNews_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdRecentNews.RowDataBound
  
          If e.Row.DataItem IsNot Nothing AndAlso e.Row.DataItem.ToString = "RssEntry" Then
              Dim curItem As RssEntry = CType(e.Row.DataItem, RssEntry)
              e.Row.Attributes.Add("href", curItem.Link) 'data-url
          End If
  
      End Sub
  </code></pre>