gpt4 book ai didi

asp.net - ASP.NET VB.NET 中的动态 ID 名称

转载 作者:行者123 更新时间:2023-12-01 04:12:48 26 4
gpt4 key购买 nike

我有大约 20 个 asp:标签在我的 ASP 页面中,全部带有 ID="lbl#" ,其中# 的范围从 0 到 22。我想动态更改他们所说的内容。虽然我可以写

lbl1.Text = "Text goes here"

对于所有 23 个,我想知道是否有办法遍历所有这些并更改其文本。

我想用我所有的标签创建一个数组,然后做一个 每个循环,但我还必须使用 检查该元素是否存在IsNothing 在我更改其文本之前,所以我被困在那里。

如果有人能帮助我,我真的很感激!

非常感谢您的帮助!!

最佳答案

您可以使用 System.Web.UI.Page.FindControl() 在页面上动态查找控件。您的 Page_Load 方法中的方法:

Dim startIndex As Integer = 0
Dim stopIndex As Integer = 22

For index = startIndex To stopIndex
Dim myLabel As Label = TryCast(FindControl("lbl" + index), Label)

If myLabel Is Nothing Then
Continue For
End If

myLabel.Text = "Text goes here"
Next

关于asp.net - ASP.NET VB.NET 中的动态 ID 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5122724/

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