【问题标题】:How to programmatically replace an HyperLinkField in a ASP.NET GridView如何以编程方式替换 ASP.NET GridView 中的 HyperLinkField
【发布时间】:2012-03-19 02:28:08
【问题描述】:

我有一个 ASP.NET Web 窗体应用程序。在我的应用程序中,我有一个运行顺畅的 GridView。我有几个文本字段,最后一个是<asp:hyperlinkfield>

现在,如果满足特定条件,我想通过放置 简单链接 而不是 hyperlinkfield 以编程方式更改字段。因此我抓住了onRowDataBound 事件:

Sub myGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles myGridView.RowDataBound

    If (condition) Then
           Dim link = New HyperLink()
           link.Text = "login"
           link.NavigateUrl = "login.aspx"
           e.Row.Cells(3).Controls.Add(link)
        End If
    End If
End Sub

其中 n 是放置hyperlinkfield 的单元格。使用此代码,它只是将新的link 添加到hyperlinkfield。怎么换?

PS:代码是VB6,但我是C#程序员,两种语言的答案都可以接受

【问题讨论】:

    标签: asp.net gridview hyperlink rowdatabound


    【解决方案1】:

    在添加新控件之前从集合中删除要替换的控件:

    protected void TestGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
        HyperLink newHyperLink = new HyperLink();
        newHyperLink.Text = "New";
        e.Row.Cells[3].Controls.RemoveAt(0);
        e.Row.Cells[3].Controls.Add(newHyperLink);
      }
    }
    

    但我同意其他人的观点,只需更改现有链接的属性:

    protected void TestGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
        HyperLink link = e.Row.Cells[0].Controls[0] as HyperLink;
        if (link != null)
        {
          link.Text = "New";
          link.NavigateUrl = "New";
        }
      }
    }
    

    【讨论】:

    • 我给你答案,但第一个就是我要找的那个。主要原因是我的HyperLinkField 具有链接到数据的其他属性,因此我不能只替换Text 和NavigateUrl。令人惊讶的是,没有人注意到我必须删除的控件是HyperLinkField,它与HyperLink 不同。我猜第一个解决方案工作顺利,除非有一些 JavaScript 连接到 GridView 的数据刷新,但我的情况并非如此。非常感谢!
    【解决方案2】:

    在这种情况下,我通常将绑定字段转换为模板字段。

     <asp:TemplateField HeaderText="Title" SortExpression="Title">
        <ItemTemplate>
           <asp:HyperLink ID="TitleHyperLink" runat="server" ></asp:HyperLink>
        </ItemTemplate>
     </asp:TemplateField>
    

    然后在代码隐藏中完成其余的工作。

    protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            var link = (HyperLink)e.Row.FindControl("TitleHyperLink");
    
            if (link != null)
            {
                if (condition)
                {
                   link.Text = "login";
                   link.NavigateUrl = "login.aspx";
                }
                else 
                {
                   link.Text = "default text";
                   link.NavigateUrl = "default.aspx";
                }
            }
        }
    }
    

    【讨论】:

    • 感谢这个解决方案很好,但令人讨厌的是超链接字段没有 Id 属性
    • @lincolnk 的意思是使用 TemplateField,然后您可以使用 HyperLink 而不是 HyperLinkField,HyperLinkField 会有一个 Id。
    【解决方案3】:

    此时不要创建新链接,而是获取已作为字段一部分生成的链接。

    If (e.Row.RowType = DataControlRowType.DataRow) Then
        Dim link = e.Row.Cells(3).Controls(0)
        link.Text = "login"
        link.NavigateUrl = "login.aspx"
    End If
    

    编辑:添加了 If 块以避免在项目行之外执行操作。

    【讨论】:

    • 谢谢,但它给了我错误 6“System.Web.UI.WebControls.HyperLinkField”类型的值无法转换为“System.Web.UI.Control”。
    • 你能说出你到底想做什么吗?
    • 谢谢。如果用户未登录,我想显示登录链接而不是下载链接
    • 此转换可能在标题行失败。请参阅上面的编辑。
    【解决方案4】:

    您可以在您的 aspx 文件中执行此操作:

    <asp:HyperLink Text='<%# condition ? "TextIfTrue" : "TextIfFalse" %>' NavigateUrl='<%# condition ? "UrlIfTrue" : "UrlIfFalse" %>' />
    

    或投你的

    e.Row.Cells(3).Controls(0)
    

    进入超链接并更改其值。

    【讨论】:

    • 谢谢。我已经考虑过了,但条件很复杂。而且我总是尽量把代码放在代码后面,不喜欢“脏”的aspx
    【解决方案5】:

    可以在aspx中使用:

    <asp:HyperLink ID="HyperLink1" CssClass="exercise" runat="server" NavigateUrl="#">Search ¡here!</asp:HyperLink>
    

    在代码隐藏中: 您还可以使用一种方法:

    public string SharePoint(string x)
    {
    
       string page1, page2;
    
                    if (x== "1")
                    {
                        string page1="http://nwpage/files.zip";
    
                        return page1;
                    }
    
                    else
                    { 
    
    
                        string page2="http://example2.aspx";
                        return page2;
                    }
    
    
            }
    

    如果我在其他方法或页面加载中调用控件,可以添加HyperLink1和路径

     string path= SharePoint(x);
     HyperLink1.NavigateUrl = path;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-13
      • 2012-03-19
      • 1970-01-01
      • 2012-06-14
      • 2010-12-04
      相关资源
      最近更新 更多