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.

   <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=**"News.aspx"**>--%>
                                            <strong><a href="News.aspx">
                                                <%#Eval("NewsDesc")%></a></strong>
                                            <br />
                                            <span class="italic">
                                                <%#Eval("NewsDate")%></span>
                                        </blockquote>
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>

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.

  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
有帮助吗?

解决方案

I found a way to insert an <%#Eval %> statement into the attribute I wanted to change. I point it towards the correct property of the rssobject, and its working.

<Columns>
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <blockquote>
                                        <strong><a href='<%#Eval("Link")%>'>
                                            <%#Eval("NewsDesc")%></a></strong>
                                        <br />
                                        <span class="italic">
                                            <%#Eval("NewsDate")%></span>
                                    </blockquote>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top