views:

899

answers:

3

Any body have idea how to put controls dynamically in asp.net 3.5 ? if any example please provide to me. another Question : is it possible to create Event for Dynamic Controls ?

+3  A: 

Here is an example (including creating an event) for ASP.NET dynamic controls...

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

    this.Form.Controls.Add(button);
}

private void ButtonClick(object sender, EventArgs e)
{
    (sender as Button).Text = "You just clicked me!";
}

Hope it helps!

Chalkey
That's right but i want that the my form will be design by the userso how many control added dynamically that is not fix.
KuldipMCA
@KuldipMCA - I think you need to provide so more detail. If you want your users to add dynamic controls at runtime then maybe web parts might be an option?
Andy Rose
I think what he want is ASP.net 3.5 DynamicControl
Muhammad Akhtar
1+ is button.ID = "button";
Muhammad Akhtar
how to get the dyanmic event ??and how we give serverside validation ??
KuldipMCA
+1  A: 

Here is an article that explains how to do it for custom usercontrols.

Sani Huttunen
+3  A: 

It is certainly possible, and it's possible to "wire up" a controls events despite it being created dynamically.

See the following links for full information:

Dynamic Controls in ASP.NET

How to: Add Controls to an ASP.NET Web Page Programmatically

Dynamic ASP.Net Control Creation Using C#.Net

How To Dynamically Add Controls to a Web Page Video

CraigTP