Asp.net dynamically generates the Button control, asp. netbutton

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.