More ParseControl()

  • Thread starter Thread starter Jeremy McPeak
  • Start date Start date
J

Jeremy McPeak

I am playing around with ParseControl(), and I have a question. First here
is the code I am using:

Control MyButton1 = Page.ParseControl("<asp:button id='MyButton'
text='Click here!' runat='server' />");
Form1.Controls.Add(MyButton1);

This adds the button to the form; however, I cannot access the button using
it's ID (and I have declared it in the class). I can, however, access it
through the Controls[] collection of Form1. The drawback is that I cannot
access any of the Button class' properties. I have tried casting it:

Button MyButton2 = (Button) MyButton1;

But I get an invalid cast error at runtime.

My question is this: Is there a way where I can access the control, as a
Button, that was parsed through ParseControl other than the Controls[]
collection of its parent?

Thanks.
 
I found the solution. It seems that I had to cast using the FindControl()
method:

Button MyButton1 = (Button)Form1.FindControl("MyButton");
 
Back
Top