Dynamic ASP.NET controls??

  • Thread starter Thread starter ªL©ú©v
  • Start date Start date
ª

ª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
 
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
 
Back
Top