Dynamic ASP.NET controls??

ª

ªL©ú©v

Dear all:
Can I use the following code to generate controls dynamically? But put
it in a form having attribute "runat=server"??
If I use the code below, it will cause some error because of myBtn
doesn't have "runat=server".

System.Web.UI.WebControls.Button myBtn = new Button();
myBtn.ID = "dynaBtn";
myBtn.Text = "dynaBtn";
myBtn.Click += new System.EventHandler(this.ClickEventHandler);
this.Controls.Add(myBtn);

If this code can work exactly, how to specify "runat=server" to myBtn?

All code above is written by C# and in code-behind.

Thanks.

Andrew Feb 17,2004
 
J

Jure Spik

Andrew, If you are creating button serverside (as you do in your example)
you do not need to set runat=server as it is implied. I think your error is
that button must be in a form with runat=server, and you are adding it to
the Page itself if that is the case you must do:
in html:
<form runat='server' id='myForm'></form>

in codebehind:
myForm.Controls.Add(myBtn)


Good luck, Jure
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top