使用 C# asp.net 在网格视图中创建超链接按钮单元格



我的网页中有一个GridView。它显示数据,列为状态, 名称 , ID 和操作。我的状态列总是随机填充 3 个值(完成、排队失败)。

现在我想将此状态列值显示为链接,如果它具有"失败"或"已排队"的值。但"完成"状态不应显示为链接。

如何在运行时实现此设计?

我将数据绑定到网格的代码是,

protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dtActionList = clsactionList.GetADActionList();
        grdADActionList.DataSource = dtActionList;
        grdADActionList.DataBind();
    }
    protected void grdADActionList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        foreach (GridViewRow gvr in grdADActionList.Rows)
        {
            if ((gvr.FindControl("Label1") as Label).Text == "Completed")
            {
                (gvr.FindControl("Label1") as Label).Visible = true;
                (gvr.FindControl("HyperLink1") as HyperLink).Visible = false;
            }
        }
    }

使用此代码,我只是简单地绑定网格中的值。我无法创建状态列,因为该列具有基于该状态列的绑定值的链接按钮。

我的.aspx代码是:

<asp:GridView ID="grdADActionList" runat="server" Height="83px" Width="935px" AutoGenerateColumns="false" OnRowDataBound="grdADActionList_RowDataBound">
     <Columns>
      <asp:TemplateField HeaderText="Status" SortExpression="Status">
            <ItemTemplate>
                 <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='http://localhost:52807/Default.aspx?'><%# Eval("Status") %>
                 </asp:HyperLink>
                 <asp:Label ID="Label1" runat="server" Text="<%# Container.DataItem %>" Visible="False"></asp:Label>
            </ItemTemplate>
      </asp:TemplateField>
      <asp:BoundField DataField="GivenName" HeaderText="GivenName"/>

请帮助我进一步做到这一点。

在 GridViewDataBound 事件上,只需隐藏链接并在值完成时显示一个简单的标签。
ASP.NET:

<asp:TemplateField HeaderText="Status" SortExpression="Status">
    <ItemTemplate>
        <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='your_url'>
            <%# Eval("Status") %>
        </asp:HyperLink>
        <asp:Label ID="Label1" runat="server" Text="<%# Eval("Status") %>" Visible="False"></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

C#:

protected void onGridViewDataBound()
{
    foreach(GridViewRow gvr in grd)
        if((gvr.FindControl("Label1") as Label).Text.ToLower() == "complete") // Updated Line
        {
            (gvr.FindControl("Label1") as Label).Visible = true;
            (gvr.FindControl("HyperLink1") as HyperLink).Visible = false;
        }
}

你必须在设计文件中工作意味着.aspx文件你必须使用和

<asp:GridView ID="GridView1" runat="server" EnableModelValidation="True">
   <asp:TemplateField HeaderText="Hyperlink">
<ItemTemplate>
    <asp:HyperLink ID="HyperLink1" runat="server" 
        NavigateUrl='<%# Eval("CODE", @"http://localhost/Test.aspx?code={0}") %>' 
        Text='link to code'>
    </asp:HyperLink>
</ItemTemplate>

您可以在 RowDataBound 事件上放置处理程序

protected void gw_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)    
    {
        DataRowView v_DataRowView = (DataRowView)e.Row.DataItem;
        string NavigateUrl = <....place your link here with DataRowView info>
        e.Row.Attributes.Add("onclick", NavigateUrl);
    }
}

现在,您已经自动生成了列,因此请先禁用该功能。然后你需要将每一列定义为一个边界字段,对于炒作链接,考虑到你的条件,最好的方法是定义模板字段:

<asp:GridView ID="grdADActionList" runat="server" BorderStyle="Double" BorderWidth="3px" 
               Height="83px" Width="935px"
               AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField DataField="Name" HeaderText="Name"/>
            <asp:BoundField DataField="Action" HeaderText="Action"/>
            <asp:BoundField DataField="Id" HeaderText="Id"/>
            <asp:TemplateField HeaderText="Status">
                <ItemTemplate>
                    <asp:HyperLink runat="server" NavigateUrl="~/link/address" Text='<%# Eval("Status") %>'
                                   Visible='<%# (int)Eval("Status") != 1 %>'/>
                    <asp:Label runat="server" Text='<%# Eval("Status") %>'
                               Visible='<%# (int)Eval("Status") == 1 %>'>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
</asp:GridView>

请注意,这只是一个变体 - 您尚未指定Status列包含哪些值,因此我假设这是基于 int 的枚举。

最新更新