Asp.net 3.5 Dynamic Controls

2019-05-31 13:10发布

问题:

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 ?

回答1:

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!



回答2:

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



回答3:

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