Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
VB
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


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
Posted
Updated 18-Apr-12 9:35am
v4
Comments
R. Giskard Reventlov 18-Apr-12 14:46pm    
Are you getting an exception? If so, what is it? Why are you appending 1 to the control names in that manner? I see you've asked the same question elsewhere.
anjumnavid 18-Apr-12 15:06pm    
Yes i asked this question elsewhere too.

Well what i am trying to do here is . i have 100 label name are lable1 to lable100. I can do like label1 =1 and label2 =2 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 every one looking to the code but this has to do with page life cycle

Thanks For the response . I found the Solution .
ASP.NET
<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
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 


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.
 
Share this answer
 
v2
When the control is rendered to html, it might have a diferent ID, the property is named ClientID, 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:
XML
<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>


and the code behind (c# in this case) is like this:
C#
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!";
        }
    }


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.
 
Share this answer
 
Comments
anjumnavid 19-Apr-12 8:49am    
Thanks For the response . I found the Solution .

<asp:Content runat="server" id="leftcontent" ContentPlaceHolderID="LeftSidePanel">
<div id="mydiv" runat="server" >
<asp:LinkButton id="Lnk_Heading1" runat="server">
<asp:Label id="lbl_Date1" runat="server" >
<asp:Label id="lbl_Detail1" runat="server" >

<asp:Button ID="Button1" runat="server" Text="Button" />
</div>



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



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 then it work.
XML
<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
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




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.
 
Share this answer
 
Is it web application or not

if yes
try to put
On formload event
if(!ispostback)
{
put your code
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900