ASP.NET-008-JS binding control event

Source: Internet
Author: User

Generally, events on the interface can be added through the event attributes inherent in the control. In fact, another way is to let JS bind events to the control, so that the control event can be dynamically changed, making it easier to implement functions.
The format of the JS binding control event is as follows:
$ ("# <% = Global Control name. clientID %> "). bind ("event name", function () {JS method (); return false;}) corresponding to the event ;});
Or
$ (Control object). bind ("event name", function () {JS method (); return false ;}) for the event ;});
The following code shows the effect of an event bound to a global button control and a row button in the Gridview.
Button

Button event in the gridview

The Code is as follows:
The front-end JS Code is mainly used for implementation. The back-end code is only responsible for displaying bound data.
Front-end,

         <Script type = "text/javascript" src = "/Scripts/Ajax. js "> </script> <script type =" text/javascript "src ="/Scripts/jquery-1.4.1.js "> </script> <script language =" javascript "type =" text /javascript "> var dgPersonsID =" <% = dgPersons. clientID %> "; $ (document ). ready (function () {$ ("# <% = btnFirst. clientID %> "). bind ("click", function () {FunctionFirst (); return false ;}); BindEventGridView () ;}); function Function First () {alert ("click button 1"); return false;} function BindEventGridView () {var dgg = document. getElementById (dgPersonsID); for (var I = 1; I <dgg. rows. length; ++ I) {var cells = dgg. rows [I]. cells; var row = dgg. rows [I]; if (row. style. display! = "None") {$ (cells [4]. firstChild ). bind ("click", function () {OperateClick (); return false ;}) ;}} return false ;}function OperateClick () {alert ("operation button is clicked! "); Return false ;}</script>
Background
Public partial class ChildFrm: System. Web. UI. Page {protected void Page_Load (object sender, EventArgs e) {if (! IsPostBack) {DataTable dt = InitData (); this. dgPersons. dataSource = dt; this. dgPersons. dataBind () ;}} private DataTable InitData () {DataTable PersonCollect = new able (); PersonCollect = new DataTable (); PersonCollect. columns. add ("p_id"); PersonCollect. columns. add ("p_name"); PersonCollect. columns. add ("p_age"); PersonCollect. columns. add ("p_sex"); if (PersonCollect. rows. count <1) {for (int I = 0; I <5; I ++) {DataRow nrow = PersonCollect. newRow (); nrow ["p_id"] = System. guid. newGuid (). toString (); nrow ["p_name"] = "northwest poplar"; nrow ["p_age"] = 27; nrow ["p_sex"] = "male"; PersonCollect. rows. add (nrow) ;}} return PersonCollect ;}}


Code download

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.