gpt4 book ai didi

asp.net - 如何以编程方式替换 ASP.NET GridView 中的 HyperLinkField

转载 作者:行者123 更新时间:2023-12-02 15:40:14 26 4
gpt4 key购买 nike

我有一个 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

其中 nhyperlinkfield 所在的单元格被放置。使用此代码,它只是添加到 hyperlinkfieldlink 。我该如何更换它?

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

最佳答案

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

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";
}
}
}

关于asp.net - 如何以编程方式替换 ASP.NET GridView 中的 HyperLinkField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9501898/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com