Add contols at run time

  • Thread starter Thread starter jatiner84
  • Start date Start date
What is process for adding text box and button at run time.

Create the controls in the Page_Init method and add them to a "container"
control, which could just be the page itself or a Panel etc

protected void Page_Init(object sender, System.EventArgs e)
{
TextBox txtMyDynamicTextBox = new TextBox();
txtMyDynamicTextBox.ID = ...;
txtMyDynamicTextBox.Width = ...;
txtMyDynamicTextBox.Visible = false;
this.Controls.Add(myDynamicTextBox);
}
 
First consider if you need it in the first place. Can you place the controls
on the form in design time and just hide them as needed? This would be a
much better design.

If you do need to create controls, you need to create one, set its
properties and add it to the Controls collection of the container object. If
you need to use the control after postbacks, you should re-create it on
every postback, preferably in Page_PreInit event.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
 
What is process for adding text box and button at run time.

TextBox t = new TextBox();
t.Text = "";
Panel1.Controls.Add(t); // to add this control into panel Panel1

the same for button
 
Howdy,

You have to recreate dynamic controls on every request (both postabck ==
true/false) Here I cerated a simple example for you:

<asp:PlaceHolder runat="server" ID="dynamicControls" />
<asp:Label runat="server" ID="label" />

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
Button button = new Button();
button.Text = "Click me!";
button.Click += new EventHandler(button_Click);

TextBox textBox = new TextBox();
textBox.ID = "DynamicTextBox";

dynamicControls.Controls.Add(button);
dynamicControls.Controls.Add(textBox);
}

private void button_Click(object sender, EventArgs e)
{
TextBox txt = (TextBox)dynamicControls.FindControl("DynamicTextBox");
if (txt != null)
{
label.Text = "You entered :" + txt.Text;
}
}

</script>
 
Hi Mark,

You cannot add textbox control to page.Controls collection directly because
you'll get an exception ('TextBox' must be placed inside a form tag with
runat=server). This is because HtmlForm is contained within controls
collection, so you could use this.Controls[3].Add() which is obviously c**p
:) or use placeholder or any container control : placeHolder1.Controls.Add()

Best Regards

Milosz
 
You cannot add textbox control to page.Controls collection directly
because
you'll get an exception ('TextBox' must be placed inside a form tag with
runat=server). This is because HtmlForm is contained within controls
collection, so you could use this.Controls[3].Add() which is obviously
c**p
:) or use placeholder or any container control :
placeHolder1.Controls.Add()

You're absolutely correct - apologies, group... :-(
 
You have to recreate dynamic controls on every request

protected void Page_Load(object sender, EventArgs e)

You shouldn't create dynamic controls in Page_Load - chances are, it'll be
too late by then, and isn't guaranteed to work...

They need to be created earlier in the Page lifecycle, e.g. Page_Init or
Page_PreInit
 
Back
Top