C # dynamically generate controls and add event processing
Sometimes the control needs to be dynamically generated when the program runs to a certain time or after an event
In C #, the general method is:
Private Button Db = new Button ();
Db. Name = "Dy_Button" // set the Name
Db. Location = new Point (100,200); // set the Location
.... // Other property settings
// Add message processing here
..
This. Controls. Add (Db); // Add to control group
This completes the dynamic generation, but you need to add a Message Processing Event to the control.
Now, you need to add the following statement before the generated control is added to the control group:
Dpb. MouseClick + = new EventHandler (this. pictureBox_MouseClick );
Now the message processing function pictureBox_Click () is compiled ()
Private void pictureBox_MouseClick (object sender, EventArgs e)
{
MessageBox. Show ("click ");
}
This completes the addition of Event Processing
First, create a global variable "I" to differentiate the buttons:
Private int I = 0;
Add the following method to the existing button:
Private void button#click (object sender, System. EventArgs e)
{
I ++;
Button B = new Button (); // create a new Button
B. Name = "B" + I; // This is the method I used to differentiate buttons.
System. Drawing. Point p = new Point (+ I * 30); // create a coordinate to locate the new button.
B. Location = p; // bind the button Location with the created Coordinate
Panel1.Controls. Add (B); // Add this button to the panel
B. Click + = new System. EventHandler (btn_click); // bind the button Method to the Click Event of the button. B. Click Is the Click Event of the button.
}
After completing the preceding steps, you can create dynamic buttons.
Next we will explain how to add the corresponding event Method btn_click () to the new button ():
Private void btn_click (object sender, System. EventArgs e)
{
Button b1 = (Button) sender; // converts the object that triggers this event to this Button object
MessageBox. Show ("" + b1.Name );
}
Now, the buttons and events for dynamic creation have been completed.
Asp.net dynamically adds events to controls
The function is to dynamically Add a Button in the Panel on the webpage and write a click event for the Button.
Dynamically add control events. Statement:
Code
Control. Command + = new CommandEventHandler (this. EventFun );
For specific code, see the following:
Note the following:
When adding controls and adding events to controls, you cannot place them in if (! IsPostback) {}. In that case, the control disappears after one click, and the event does not exist.
Yes.
Code
Protected void Page_Load (object sender, EventArgs e)
{
// Analyze the input string as a System. Web. UI. Control object, and B as the passed value.
Control c = ParseControl ("<asp: Button Text = ''ID = 'mycall' commandargument =' B 'runat = 'server'/> ");
// Add the widget to the large Panel
This. Panel1.Controls. Add (c );
// Find the control named myButton on the page
Button = (Button) Page. FindControl ("myButton ");
// Add event On_Button
Button. Command + = new CommandEventHandler (this. On_Button );
}
// CommandEventArgs provides data for command events
Protected void On_Button (Object sender, CommandEventArgs e)
{
Response. write ("<script language = 'javascript 'Type = 'text/javascript '> alert ('" + e. commandArgument. toString () + "'); </script> ");
}
Http://www.cnblogs.com/zhuimengke/articles/1283973.html
Http://www.cnblogs.com/xuasmi/
Add controls and event categories dynamically: asp. net2007.5.15 by: Little Monster | comment: 1 | read: 1393
Private void Page_Load (object sender, System. EventArgs e)
{
Button Button1 = new Button ();
Button1.CommandArgument = "b1 ";
Button1.Text = "Btn1 ";
Button1.Command + = new CommandEventHandler (this. OnButton );
PlaceHolder1.Controls. Add (Button1 );
Button Button2 = new Button ();
Button2.CommandArgument = "b2 ";
Button2.Text = "Btn2 ";
Button2.Command + = new CommandEventHandler (this. OnButton );
PlaceHolder1.Controls. Add (Button2 );
Control c3 = ParseControl ("<asp: Button id = 'button3' text = 'btn3' commandname = 'btn 'commandargument = 'b3 'runat = 'server'/> "); // convert a string to a web Control
Control c4 = ParseControl ("<asp: Button id = 'button4' text = 'btn4' commandname = 'btn 'commandargument = 'b4 'runat = 'server'/> ");
PlaceHolder1.Controls. Add (c3 );
PlaceHolder1.Controls. Add (c4 );
Button myBut = (Button) Page. FindControl ("Button3 ");
MyBut. Command + = new CommandEventHandler (this. OnButton );
Button myBut2 = (Button) Page. FindControl ("Button4 ");
MyBut2.Command + = new CommandEventHandler (this. OnButton );
}
Public void OnButton (Object Sender, CommandEventArgs e)
{
Switch (e. CommandArgument. ToString (). ToLower ())
{
Case "b1 ":
Label1.Text = "Button 1 ";
Break;
Case "b2 ":
Label1.Text = "Button 2 ";
Break;
Case "b3 ":
Label1.Text = "Button 3 ";
Break;
Case "b4 ":
Label1.Text = "Button 4 ";
Break;
};
}
I have been busy for a long time. Today, the project is almost finished and can be easily completed. Tomorrow's weekend.
In my project, I encountered the dynamic creation of the Tab and the Gridview. The following method can be used to create them, of course, in page_load. Sometimes, it will cause other buttons on your page to be unavailable, at this time, you can put them in page_load instead of page_init, so there will be no problem. I don't know if other Ajax controls will have such a problem. When I create a TabPanel, an exception occurs. I put them in Page_Init.
Dynamically Add the Tab control of AjaxControl Toolkit and set the Template
A friend is using the Tab control in the AjaxControl Toolkit. He just asked how to dynamically add TabPanel items in cs. I created a project and tried it. The main problem is how to create a ContentTemplate in TabPanel.
<AjaxToolkit: TabContainer> the control is easy to use. Drag it to the page, add a TabPanel, and enter the content of the TabPanel directly. The control fully supports WYSIWYG during design, which is very convenient. The content in the TabPanel is specified through <contenttemplate> </contenttemplate>. This corresponds to the ContentTemplate attribute of TabPanel. In VS, intelliisense shows that its type is ITemplate. This is an interface. I tried to find out whether a class has implemented this interface in intelliisense, but nothing was found. If you cannot find this class, you cannot assign a value to ContentTemplate. So I checked the source code of the Tab control, checked MSDN, and finally solved the problem.
The key problem is that this class implements the ITemplate interface and I can't find it, so I had to write one myself. I don't know if there are any other methods. I hope some friends will tell me. The Code is as follows:
Using System;
Using System. Data;
Using System. Configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using AjaxControlToolkit; // remember to add this.
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
TabContainer tabContainer1 = new TabContainer ();
TabPanel tab1 = new TabPanel ();
Tab1.HeaderText = "label 1 ";
TabContainer1.Tabs. Add (tab1 );
TabPanel tab2 = new TabPanel ();
Tab2.HeaderText = "label 2 ";
TabContainer1.Tabs. Add (tab2 );
// Create the content in the first Tab
Panel panel1 = new Panel ();
Label label1 = new Label ();
Label1.Text = "This is the first Tab ";
Panel1.Controls. Add (label1 );
// Create a Template
TabContentTemplate temp1 = new TabContentTemplate ();
// Add a child Control
Temp1.SetTemplate (panel1 );
Tab1.ContentTemplate = temp1;
This. form1.Controls. Add (tabContainer1 );
}
}
Public class TabContentTemplate: ITemplate
{
Private Control _ template;
Public void SetTemplate (Control templateControl)
{
_ Template = templateControl;
}
ITemplate Members # region ITemplate Members
Public void InstantiateIn (Control container)
{
Container. Controls. Add (_ template );
}
# Endregion
}
Many controls, such as GridView and Repeat, all use templates. You can also use this method to dynamically set templates.
BTW, another way to dynamically set the template is to use Page. LoadTemplate ("template. ascx"), such as tab1.ContentTemplate = Page. LoadTemplate ("template. ascx ")