如何从页面加载后的代码中获取控件ID [英] How to get Controls ID from code behind on page load
问题描述
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim lbl_Date As New Label
Dim i as int16
i = 20
While (i < i + 1)
lbl_Date = TryCast(Page.FindControl("lbl_Date" + i.ToString), Label)
lbl_Date = "Value"
i = i + 1
End While
End Sub
这在lbl_Date变量中什么也没有返回.如果我在`Page_Load`中不使用它,它就可以正常工作.仅在"Page_Load"中无效.
说明 ::::我想在这里做的是.我有100个标签名称是lable1到lable100.我可以像label1 = Value和label2 = Value之类的来做,但是我正试图放入循环中,所以我的代码更少.仅用于信息... 此代码有效,但不能在Page_Load中使用,我认为这与页面生命周期有关
This is returning Nothing in `lbl_Date` variable. If i don''t use this in `Page_Load`, it works just fine. Only in `Page_Load` it doesn''t work.
Explanation :::: Well what i am trying to do here is . i have 100 label name are lable1 to lable100. I can do like label1 =Value and label2 =Value and so on but i am trying to put in loop so i have less code. just for the Info... This code work but not in Page_Load , I am thinking this has to do with page life cycle
推荐答案
感谢您的回复.我找到了解决方案.
Thanks For the response . I found the Solution .
<asp:content runat="server" id="leftcontent" contentplaceholderid="LeftSidePanel" xmlns:asp="#unknown">
<div id="mydiv" runat="server">
<asp:linkbutton id="Lnk_Heading1" runat="server"> </asp:linkbutton>
<asp:label id="lbl_Date1" runat="server"> </asp:label>
<asp:label id="lbl_Detail1" runat="server"> </asp:label>
<asp:button id="Button1" runat="server" text="Button" />
</div>
</asp:content>
Vb.net *******
Vb.net *******
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim lbl_Date As New Label
Dim i as int16 i = 20
While (i < i + 1) lbl_Date = TryCast(Mydiv.FindControl("lbl_Date" + i.ToString), Label)
lbl_Date = "Value"
i = i + 1
End While
End Sub
问题是,我没有在aspx页面中使用DIV.当我将代码放在div中并在代码后面使用div的引用时,它就起作用了.
Problem was , I didn''t Used DIV in my aspx page . When i put my code in div and use the reference of the div in codebehind then it work.
当控件呈现为html时,它可能具有不同的ID,该属性被命名为ClientID
,但是如果您只想更改所有控件,只需将它们放在容器中并检查它们即可.
在asp.net上将如下所示:
When the control is rendered to html, it might have a diferent ID, the property is namedClientID
, but if you want just to change all controls, just place them inside a container and check for them.
on the asp.net would be like this:
<div id="mycontainer" runat="server">
<asp:Label runat="server" ID="label1" Text="label 1"></asp:Label>
<asp:Label runat="server" ID="label2" Text="label 2"></asp:Label>
<asp:Label runat="server" ID="label3" Text="label 3"></asp:Label>
<asp:Label runat="server" ID="label4" Text="label 4"></asp:Label>
<asp:Label runat="server" ID="label5" Text="label 5"></asp:Label>
<asp:Label runat="server" ID="label6" Text="label 6"></asp:Label>
<asp:Label runat="server" ID="label7" Text="label 7"></asp:Label>
<asp:Label runat="server" ID="label8" Text="label 8"></asp:Label>
</div>
而后面的代码(在这种情况下为C#)是这样的:
and the code behind (c# in this case) is like this:
protected void Page_Load(object sender, EventArgs e)
{
foreach (var control in mycontainer.Controls)
{
Label label = control as Label;
if (label != null) label.Text = "Found it!";
}
}
那应该起作用.
请注意,如果您在页面上附加了任何其他逻辑,则可能需要在PreRender
事件而不是Page_Load
事件中执行此操作.
希望对您有所帮助.
That should work.
Please notice that if you attach any other logic to your page, you might want to do that in the PreRender
event, instead of the Page_Load
.
Hope it helps.
<asp:content runat="server" id="leftcontent" contentplaceholderid="LeftSidePanel" xmlns:asp="#unknown">
<div id="mydiv" runat="server">
<asp:linkbutton id="Lnk_Heading1" runat="server"> </asp:linkbutton>
<asp:label id="lbl_Date1" runat="server"> </asp:label>
<asp:label id="lbl_Detail1" runat="server"> </asp:label>
<asp:button id="Button1" runat="server" text="Button" />
</div>
</asp:content>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim lbl_Date As New Label
Dim i as int16 i = 20
While (i < i + 1) lbl_Date = TryCast(Mydiv.FindControl("lbl_Date" + i.ToString), Label)
lbl_Date = "Value"
i = i + 1
End While
End Sub
问题是,我没有在aspx页面中使用DIV.当我将代码放在div中并在代码后面使用div的引用时,它就起作用了.
Problem was , I didn''t Used DIV in my aspx page . When i put my code in div and use the reference of the div in codebehind then it work.
这篇关于如何从页面加载后的代码中获取控件ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!