jQuery is a very powerful client-side JS programming technology. I don’t want to elaborate too much on its related background knowledge here. I just want to briefly demonstrate how to develop it in conjunction with asp.net controls.
For example, we want to do a function as shown in the figure below. The effect is that all items of status, number, number 1, number 2, and average are bound through the background. How to click the checkbox button to automatically calculate the current row two What is the average of the numbers? The premise is to use jQuery to achieve?
We directly enter the following code in the Page_Load event of the page:
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { new DataColumn("id",typeof(Int32)), new DataColumn("num1",typeof(Int32)), new DataColumn("num2",typeof(Int32)) }); DataRow dr = null; dr = dt.NewRow(); dr["id"] = 1; dr["num1"] = 20; dr["num2"] = 40; dt.Rows.Add(dr); dr = dt.NewRow(); dr["id"] = 2; dr["num1"] = 40; dr["num2"] = 30; dt.Rows.Add(dr); this.GridView1.DataSource = dt.DefaultView; this.GridView1.DataBind(); } }
Front page body part:
<body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> <Columns> <asp:TemplateField HeaderText="状态"> <ItemTemplate> <asp:CheckBox ID="checkstate" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="编号"> <ItemTemplate> <asp:Label ID="lblId" runat="server"> <%#Eval("id") %></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="数字1"> <ItemTemplate> <asp:Label ID="lblNum1" runat="server"> <%#Eval("num1") %></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="数字2"> <ItemTemplate> <asp:Label ID="lblNum2" runat="server"> <%#Eval("num2") %></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="平均值"> <ItemTemplate> <asp:TextBox ID="avg_value" runat="server" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> </form> </body>
The key is in the head part of the page. Enter the following code to achieve the effect as shown.
<script src="js/jquery-1.4.2.js"></script> <script type="text/javascript"> $(function () { $("#<%=GridView1.ClientID%>").find("tr td input[type=checkbox]").each(function () { $(this).bind("click", function () { if (this.checked) { var id = $(this).parent().parent().find("span[id*=lblId]").text(); var num1 = $(this).parent().parent().find("span[id*=lblNum1]").text(); var num2 = $(this).parent().parent().find("span[id*=lblNum2]").text(); var result = (parseFloat(num1) + parseFloat(num2)) / 2; $(this).parent().parent().find("input[id*=avg_value]").val(result); } else { $(this).parent().parent().find("input[id*=avg_value]").val(""); } }); }); }); </script>
You will find that jQuery code is easy to read and easy to understand. And the code is also very beautiful, and the most important thing is that it has good compatibility.
Attached is a simpler example. This is a static html page to see how jQuery can exert its power. The effect is that when the button of each row is clicked, the value in the text of the current row pops up.
<html> <head> <script type="text/javascript" src="jquery-1.4.2.js"></script> <script type="text/javascript"> $(function(){ $("table tr td").each(function(){ $(this).find("[type=button]").click(function(){ alert($(this).parent().parent().find("[type=text]").val()); }); }); }); </script> </head> <body> <table> <tr> <td>1</td> <td><input type=text value="数据1" /></td> <td><input type=button onclick="GetTest()" value="获取" /></td> </tr> <tr> <td>2</td> <td><input type=text value="数据2" /></td> <td><input type=button onclick="GetTest()" value="获取" /></td> </tr> </table> </body> </html>
Just imagine, if we use js to do it, it will be very troublesome, and we also have to consider the compatibility of various browsers. Seeing this, I have to sigh, although jQuery is short, it is quite powerful.