JS gets information on the asp.net (C #) page on the GridView selected line

Source: Internet
Author: User

Do Web development or novice me, in order to get the value in the GridView, it was to save the data in the GridView to the server via the server-side control, then bind to a server-side hidden field, then read the values in the hidden fields through JS, and now it feels stupid. , although it is easy to get the data is simply to traverse the GridView, but consider the performance, I would like to use JS to read. Now I can get the data I want, in order to facilitate the same novice as me, now easy to write down, hope to be able to find a better solution. If you have a better idea please contact qq:643166601, I will record the update in time.

1. Prepare the database, my database name is called LIONDB, there is a table below called user, as follows:


The user table is defined as follows:


The data in the user table is as follows:



2. Place two controls on the page

A) GridView, binding data source, adding checkbox columns, other columns automatically bound

b) Input (Button), add an event (for the next specific line or a few lines of data to prepare)

After that, the page effect is as follows:


4.js Key Code

    <script language= "javascript" type= "Text/javascript" >
//<![ cdata[

        function Btnchecked_onclick () {

            var TB = document.getElementById ("<%=gridview1.clientid%>"); 
            var sline = "";
            if (TB) {
                var rows = tb.rows;
                for (var i = 1; i < rows.length i++) {
                    var cbx = rows[i].cells[0].childnodes[0].nextelementsibling;
                    if (Cbx.type = = ' checkbox ' && cbx.checked = = True) {
                        var suserid = rows[i].cells[1].innerhtml;
                        var susername = rows[i].cells[2].innerhtml;
                        Sline + + "UserId:" + sUserID + "UserName:" + sUserName + "";
                    }
                }
          alert (sline);
            }
        }
]]>
    </script>

5. Operation effect



6. Get the results you want



SOURCE Package:

Click to open the link




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.