PC Review


Reply
Thread Tools Rate Thread

Add contols at run time

 
 
jatiner84@gmail.com
Guest
Posts: n/a
 
      31st Jan 2007
What is process for adding text box and button at run time.

 
Reply With Quote
 
 
 
 
Joel Silva
Guest
Posts: n/a
 
      31st Jan 2007
Hello,

maybe this helps:

http://support.microsoft.com/kb/308433 for windows forms.

http://msdn2.microsoft.com/en-us/library/kyt0fzt1.aspx for web pages.

joel


(E-Mail Removed) wrote:
> What is process for adding text box and button at run time.
>

 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      31st Jan 2007
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

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


 
Reply With Quote
 
Eliyahu Goldin
Guest
Posts: n/a
 
      31st Jan 2007
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


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> What is process for adding text box and button at run time.
>



 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      31st Jan 2007

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
Posts: n/a
 
      31st Jan 2007
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>
--
Milosz


"(E-Mail Removed)" wrote:

> What is process for adding text box and button at run time.
>
>

 
Reply With Quote
 
=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
Posts: n/a
 
      31st Jan 2007
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

--
Milosz


"Mark Rae" wrote:

> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
> > 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);
> }
>
>
>

 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      31st Jan 2007
"Milosz Skalecki [MCAD]" <(E-Mail Removed)> wrote in message
news:17642F18-8764-443D-8D46-(E-Mail Removed)...

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


 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      31st Jan 2007
"Milosz Skalecki [MCAD]" <(E-Mail Removed)> wrote in message
news:8E84FA44-5156-4ACA-88A0-(E-Mail Removed)...

> 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


 
Reply With Quote
 
=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
Posts: n/a
 
      31st Jan 2007
Now, it's time for my aplogies - i apologize group ;-)
--
Milosz


"Mark Rae" wrote:

> "Milosz Skalecki [MCAD]" <(E-Mail Removed)> wrote in message
> news:8E84FA44-5156-4ACA-88A0-(E-Mail Removed)...
>
> > 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
>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel VBA Populatig UserFrom Contols as Design Time. John Howard Microsoft Excel Programming 3 21st Mar 2010 10:08 AM
standard accounts with parental time contols =?Utf-8?B?bW9tbXkgb2YgdHdv?= Windows Vista Administration 0 14th Nov 2007 05:01 AM
Contols and the <object> tag Sailor Foley Microsoft ASP .NET 3 16th Feb 2004 12:00 AM
How to position web server contols at run time? andrew Microsoft VB .NET 1 13th Feb 2004 03:30 PM
Re: Using Devexpress contols Dmitriy Lapshin [C# / .NET MVP] Microsoft C# .NET 0 18th Aug 2003 12:02 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:38 PM.