【问题标题】:Create hyperlink button cells in gridview using C# asp.net使用 C# asp.net 在 gridview 中创建超链接按钮单元格
【发布时间】:2014-02-21 09:23:48
【问题描述】:

我的网页中有一个 GridView。它以 Status、Name、Id 和 Action 列显示数据。我的状态栏总是随机填充 3 个值(Complete、QueuedFailed)。

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

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

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

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"/>

请帮我进一步做这件事......

【问题讨论】:

  • grdActionList 的标记是什么样的?
  • @Andrei:你是说网格的设计吗?
  • 是的。 Grid相关的aspx文件内容
  • 它只是一个网格,取自工具并与其中的 Datatable 值绑定....如何根据单元格值创建模板列作为链接​​
  • 现在您的所有列都是自动生成的。如果您对默认网格外观没问题,这很好,但是当您需要自定义时,您应该切换到手动列声明

标签: c# asp.net gridview hyperlink


【解决方案1】:

在 GridViewDataBound 事件中,如果值为complete,则隐藏链接并显示一个简单的标签。
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;
        }
}

【讨论】:

  • 我试过你的代码,它为整个列创建超链接,完成状态也显示为链接
  • 您的asp:GridView 标签中有OnDataBound="onGridViewDataBound" 吗?否则它不会触发 C# onGridViewDataBound() 事件处理程序,如果 status = "completed" 则隐藏链接。
  • 是的,我的代码中有这个。我将编辑我的帖子并更新我的最新代码...问题是, OnDataRowBound 事件没有进入条件 if((gvr.FindControl("Label1") as Label).Text == "Complete")
  • 请看我已经更新了 C# 代码,上面写着“更新的行”。使用那条线,它会起作用。
  • 问题是,gvr.FindControl("Label1") as Label).Text 没有返回单元格值。当我在标签控件 Text="" 中使用以下属性时,我遇到了一些格式错误。所以我把它改成了 Text=""
【解决方案2】:

你必须在设计文件中工作意味着 .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>

【讨论】:

    【解决方案3】:

    您可以在 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);
        }
    }
    

    【讨论】:

      【解决方案4】:

      现在您的列已自动生成,因此请先禁用该功能。然后你需要将每一列定义为一个BoundField,对于hypelink,考虑到你的情况,最好的方法是定义模板字段:

      <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 的枚举。

      【讨论】:

      • 请注意她已明确说明我的状态栏总是随机填充 3 个值(完成、排队和失败)。
      • @P5Coder,好吧,我理解它是因为该列可以包含三个值之一,具体取决于显示或不显示哪个超链接。这是不正确的吗?
      • 她已经自动生成了这些列,这意味着她显然没有为这些链接指定任何urls。但我认为超链接再次取决于价值。
      • 我对此有一个疑问。超链接和链接按钮是否相同?我需要特定单元格上的链接按钮而不是超链接。
      • @Suryakavitha、HyperlinkLinkButton 有点不同,行为也不同。基本上HyperLink 是一个简单的链接,而LinkButton 的行为就像一个按钮,尽管在页面上它也显示为一个链接。例如,单击 ob 超链接将导致重定向,而单击链接按钮会导致当前页面的回发。您应该使用适合您需求的那个
      猜你喜欢
      • 1970-01-01
      • 2011-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多