creating controls dynamically, newbie question (web project)

  • Thread starter Thread starter Marius Horak
  • Start date Start date
M

Marius Horak

If in Design mode I place a button HTML code will look like this:

<form id="Test" method="post" runat="server">
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 83px; POSITION:
absolute; TOP: 173px" runat="server"
Text="Button" Width="129px" Height="31px"></asp:Button>
</form>
How can I replicate this using C# - placing a button at selected location on
a page?

In Page_Load I tried:

Button X = new Button();
X.ID ="X";
X.Text="Button X";
X.Width=129;
X.Height=31;
this.Controls.Add(X);

But did not get anything.

So I tried to put the above code inside CreateChildControls - nothing
happened.

Thanks

MH
 
Hi Marius,

I just tested your code and it throw an exception:
"Control 'X' of type 'Button' must be placed inside a form tag with
runat=server. "

So what I did was declare the Form control in the code behind:
protected System.Web.UI.HtmlControls.HtmlForm userhome;

and change the line:
this.Controls.Add(X);

to :
userhome.Controls.Add( X);


Hope this help,
 
Thank you,

But how to make userhome to point to my form (form id="Test")?
And how to set position (I'm getting that Style is read only)?

MH
 
I managed to find form "Test" and the button is now visible but how to place
it at specific location?

MH
 
Back
Top