在gridview中插入链接

本文关键字:链接 插入 gridview | 更新日期: 2023-09-27 18:22:14

我有一个网格,它有大约16个文档的列表,这些文档可以根据使用应用程序的人的不同而更改。我被要求将网格中的三个特定条目(如果存在)更改为打开文档的链接。

如何在网格中检查这三个文档(列称为"工件"),并为这三个文件中的每一个插入正确的链接,而不是默认文本?

    <asp:BoundField HeaderText="Artifact" DataField="ArtifactName" Visible="true" HeaderStyle-Width="300px"  HeaderStyle-HorizontalAlign="Left"></asp:BoundField>

我们网站的其他部分也提供了相同的链接。以下是它们在其他页面上的实现方式

<asp:LinkButton 
ID="hypLnkAffidRelease2" 
runat="server" 
Text="Affidavit and Release form" 
/>

var url = ResolveUrl("~/FormViewer.aspx");
       this.lnkDownloadReleasefrm.Attributes.Add("onclick", " { popup=window.open('" + url + "?Form=4','Viewer','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no, width=800, height=600'); popup.focus(); return false; }");

在gridview中插入链接

您可以使用两个ItemTemplate(Label&HyperLink)创建TemplateField

渲染后,标签或多或少类似于BoundField。

<asp:TemplateField HeaderText="Select">
    <ItemTemplate>
        <asp:Label ID="NoLink" runat="server"></asp:Label>
        <asp:LinkButton ID="WithLink" runat="server" OnClick="Go_Click"/>
    </ItemTemplate>               
</asp:TemplateField>

当您绑定gridview 时

GridView.DataSouce = theData;
GridView.DataBind();
//index refers to the column number of the template field
for (int i=0; i<in GridView.Rows.Count; i++)
{
    Label a = (Label)GridView.Rows[i].Cells[index].FindControl("NoLink");
    LinkButton b = (LinkButton)GridView.Rows[i].Cells[index].FindControl("WithLink");
    if (// link exists)
    {
        a.Visible = false;
        b.Visible = true;
    }
    else)
    {
        a.Visible = true;
        b.Visible = false;
    }  
}