C# 动态更改ASP.net gridview itemTemplate中超链接的URL

C# 动态更改ASP.net gridview itemTemplate中超链接的URL,c#,asp.net,gridview,code-behind,itemtemplate,C#,Asp.net,Gridview,Code Behind,Itemtemplate,我正在创建一个UI,其中包含gridView中的itemTemplate。在ItemTemplate中,我需要一个链接,该链接将根据绑定到表的表值动态填充。换句话说,有时候链接会指向我服务器上的一个文件,有时候它会指向另一个URL。本质上,我需要能够检查绑定到gridview的表上的标志,然后根据表中对应行的数据更新每个行itemTemplate 到目前为止,我有以下标记: <asp:GridView ID="grdVDocuments"

我正在创建一个UI,其中包含gridView中的itemTemplate。在ItemTemplate中,我需要一个链接,该链接将根据绑定到表的表值动态填充。换句话说,有时候链接会指向我服务器上的一个文件,有时候它会指向另一个URL。本质上,我需要能够检查绑定到gridview的表上的标志,然后根据表中对应行的数据更新每个行itemTemplate

到目前为止,我有以下标记:

                 <asp:GridView ID="grdVDocuments" 
                                runat="server" 
                                DataSourceID="sqlDS_wwso" 
                                EnableModelValidation="True" 
                                AutoGenerateColumns="False" 
                                OnRowDataBound="grdVDocuments_RowDataBound"                      
                                CssClass="documents_DataTable" AllowPaging="True"            
                                >
                    <Columns>                            
                        <asp:TemplateField HeaderText="Download">
                            <ItemTemplate>

                                <a href="/<%# Eval("fileName") %>" target="_blank" id="lnkContent">
                                   <img src="images/orange_download_cropped.png" alt="" border="0"/></a> 

                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="displayName" HeaderText="displayName" SortExpression="displayName" />
                        <asp:BoundField DataField="fileName" HeaderText="fileName" SortExpression="fileName" />                             
                        <asp:BoundField DataField="category" HeaderText="category" SortExpression="category" />
                        <asp:BoundField DataField="sub_category" HeaderText="Sub-Category" SortExpression="sub_category" />
                        <asp:BoundField DataField="datePosted" HeaderText="datePosted" SortExpression="datePosted" />                                                         
                    </Columns>
                 </asp:GridView>

这个代码隐藏在后面,因为它无法识别超链接,所以被轰炸了

    protected void grdVDocuments_RowDataBound(object sender, GridViewRowEventArgs e)        
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            HyperLink myHyperLink = e.Row.FindControl("lnkContent") as HyperLink;

            myHyperLink.NavigateUrl = "<someOtherURL>";

        }
    }
protectedvoid grdVDocuments\u RowDataBound(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
HyperLink myHyperLink=e.Row.FindControl(“lnkContent”)作为超链接;
myHyperLink.NavigateUrl=“”;
}
}
数据表有一个名为isFile的标志,它是“位”数据类型。当isFile=1时,URL需要为“../”,否则,需要将每行的itemtemplate超链接中的URL设置为我的表中包含URL的另一个字段;i、 e.“其他网址”

感谢您的帮助;)


谢谢

您需要在ItemTemplate中使用服务器控件。因此,您需要将
runat=“server”
属性添加到
a
标记中。但是,它是一个
HtmlAnchor
元素,您需要将其转换为
HtmlAnchor
,而不是
Hyperlink
类似(代码隐藏):

。。。
var myHyperLink=e.Row.FindControl(“lnkContent”)作为HtmlAnchor;
myHyperLink.HRef=“”;
...
或者,您可以在aspx标记中使用
标记(而不是html锚定
a

...
var myHyperLink = e.Row.FindControl("lnkContent") as HtmlAnchor;
myHyperLink.HRef = "<someOtherURL>";
...