Asp.net dynamically generates the Button control, asp. netbutton
1. dynamically generate button controls and respond to server and client events
Void BindButtons ()
{
Foreach (var item in items) {Button Btn = new Button (); Btn. onClientClick = "showLoading (); return true;"; // client event // Btn. attributes. add ("OnClientClick", "this. disabled = true; return true; "); // invalid // Btn. useSubmitBehavior = false;/* if the default value is true, the type of the button = submit triggers form submission; when the value is false, the type = button of the button in the html is executed through js _ doPostBack (...) when the value is false, the function in OnClientClick must return true to submit form */Btn. width = 180; Btn. height = 180; Btn. style. add ("margin", "10px"); Btn. ID = item. UUId; Btn. text = item. orgName; Btn. click + = new EventHandler (Btn_Click); this. div1.Controls. add (Btn );}
}
void Btn_Click(object sender, EventArgs e) {
var btn = sender as Button; Utils.JSExecutor.Execute(this,"alert('"+btn.Id+"')"); }
2. call:
Protected void Page_Load (object sender, EventArgs e) {BindButtons ();/* Note that the dynamically created button cannot be placed in the following if; otherwise, the server event cannot be executed */if (! IsPostBack) {BindData ();}}
From: http://www.cnblogs.com/xuejianxiyang/p/5549697.html