Adding link dynamically to Template Field

akiramo

New Member
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. 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. \[code\] <asp:GridView ID="grdRecentNews" runat="server" AutoGenerateColumns="False" DataKeyNames="Key" GridLines="None" PageSize="4" ShowHeader="False" AllowPaging="True"> <PagerSettings Visible="False" /> <Columns> <asp:TemplateField> <ItemTemplate> <blockquote> <%-- <strong><a href=http://stackoverflow.com/questions/15892510/**"News.aspx"**>--%> <strong><a href="http://stackoverflow.com/questions/15892510/News.aspx"> <%#Eval("NewsDesc")%></a></strong> <br /> <span class="italic"> <%#Eval("NewsDate")%></span> </blockquote> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>\[/code\]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.\[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\]
 
Back
Top