More ParseControl()

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.
 
J

Jeremy McPeak

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top