我正在创建一个包含gridView中的itemTemplate的UI。在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>";
}
}
数据表上有一个名为isFile的标志,它是“bit”dataType。当isFile = 1时,URL需要为“... /”,否则每行的itemtemplate超链接中的URL需要设置为我的表中包含URL的另一个字段;即“someOtherURL”。
感谢任何帮助;)
谢谢!
答案 0 :(得分:1)
您需要在ItemTemplate中使用服务器控件。因此,您需要将runat="server"
属性添加到a
标记。但是,它是一个HtmlAnchor
元素,您需要转换为HtmlAnchor
而不是Hyperlink
(代码隐藏):
...
var myHyperLink = e.Row.FindControl("lnkContent") as HtmlAnchor;
myHyperLink.HRef = "<someOtherURL>";
...
或者,您可以在aspx标记中使用<asp:Hyperlink>
标记(而不是html anchor a
)