Add contols at run time

M

Mark Rae

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);
}
 
E

Eliyahu Goldin

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
 
G

Guest

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
 
G

Guest

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>
 
G

Guest

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
 
M

Mark Rae

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... :-(
 
M

Mark Rae

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
 

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
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top