Jump to content

Hello - Finding a dynamic loaded control...

- - - - -

  • Please log in to reply
2 replies to this topic

#1
ManyTimes

ManyTimes

    Newbie

  • Members
  • PipPip
  • 14 posts
I have this in my aspx-file:
<asp:PlaceHolder runat="server" ID="ph1" ></asp:PlaceHolder>
<asp:Button runat="server" ID="btnCheck" Text="Check" onclick="btnCheck_Click" />
Then in the code behind I do this:
protected void [COLOR=RoyalBlue]Page[/COLOR]_[COLOR=RoyalBlue]Load[/COLOR](object sender, EventArgs e)
{
       Label lbl = new Label();
       lbl.Text = "<[COLOR=RoyalBlue]label [/COLOR]id='[COLOR=RoyalBlue]lbl2[/COLOR]' runat='server'>Hello2</[COLOR=RoyalBlue]label[/COLOR]>";
       lbl.ID = "lbl1";
       ph1.Controls.Add(lbl);
}

protected void [COLOR=RoyalBlue]btnCheck[/COLOR]_[COLOR=RoyalBlue]Click[/COLOR](object sender, EventArgs e)
{
       Label tempLBL = (Label)FindControl("[COLOR=RoyalBlue]lbl2[/COLOR]");
       if(tempLBL != null)
       {
              btnCheck.Text += "FOUND1";
       }
       HtmlGenericControl htmlGC= (HtmlGenericControl)FindControl("[COLOR=RoyalBlue]lbl2[/COLOR]");
       if (htmlGC!= null)
       {
              btnCheck.Text += "FOUND2";
       }
       
       Control c = (Control)FindControl("[COLOR=RoyalBlue]lbl2[/COLOR]");
       if(c != null)
       {
              btnCheck.Text += "C";
       }
}
Problem is: I am having a hard time finding the "lbl2", which is added within the label "lbl1" in the Page_Load... None of the checks in the "btnCheck"-click goes "true", hence the "lbl2" is not found in C#, but I am able to find it in JavaScript, but that's not what I want.
Also: The output of the html creates a <span> tag surrounding the two labels...
Suggestions on how I can find this label which is added like this?

Do note; simplified example... :)

#2
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
Isn't FindControl as method invoked on a Generic control collection or a page ?
I think the syntax over here is wrong
HtmlGenericControl htmlGC= (HtmlGenericControl)FindControl("[COLOR=RoyalBlue]lbl2[/COLOR]");


#3
ManyTimes

ManyTimes

    Newbie

  • Members
  • PipPip
  • 14 posts

gokuajmes said:

Isn't FindControl as method invoked on a Generic control collection or a page ?
I think the syntax over here is wrong
HtmlGenericControl htmlGC= (HtmlGenericControl)FindControl("[COLOR=RoyalBlue]lbl2[/COLOR]");
Yes, the function "FindControl" does only exist for control collections or pages...
But since this is in a .aspx file, not a user control that is loaded dynamically or not... These calls are "equal":
Button btn= (Button)[COLOR=RoyalBlue]Page[/COLOR].[COLOR=RoyalBlue]FindControl[/COLOR]("btn");
Button btn = (Button)[COLOR=RoyalBlue]FindControl[/COLOR]("btn");
Button btn= (Button)[COLOR=RoyalBlue]ControlCollection[/COLOR].[COLOR=RoyalBlue]FindControl[/COLOR]("btn"); // a panel, placeholder...
Button btn= (Button)[COLOR=RoyalBlue]myForm[/COLOR].[COLOR=RoyalBlue]FindControl[/COLOR]("btn");  //the form variable...
If the Control "btn" was loaded from a user control; I would need to do something like this (for example, are other ways):
Button btn= (Button)myForm.FindControl("btn");
Multiple ways to do stuff, but issue is:
I cannot find the label, or any control that is create like this (through VB/C# that is):
Label lbl = new Label();
lbl.Text = "[COLOR=RoyalBlue]<label id='lbl2' runat='server'>[/COLOR]Hello2</[COLOR=RoyalBlue]label[/COLOR]>";
lbl.ID = "lbl1";
ph1.Controls.Add(lbl);
And yes, the source code shows that "lbl2" exists, not that it has been truncated, so the "Hello2" text is actually from the "lbl2" and not "lbl1"...
Was hoping for an easier solution than what I already have...




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users