Jquery is an excellent javascript framework, which greatly improves the simplicity of javascript code and reduces the difficulty of javascript. For more information about jquery functions, see http://www.bkjia.com/kf/201204/410921.html. For ajax in jquery, I mainly use the ajax () function. I will write a bit of usage experience below. I would like to ask you more about any shortcomings. The background code is C #
1. Add data to a table
Front-end code
1 $. ajax ({
2 type: "POST", // access WebService using Post request
3 contentType: "application/json", // WebService returns the Json type
4 url: "ajax. aspx/AddToTP ", // WebService call address and method name combination ---- WsURL/method name 5 data: '{tid:"' + tid + '", uid: "'+ uid +'", score: "'+ score +'", degree: "'+ degree +'"} ', // The parameter to be passed here, the format is data: "{paraName: paraValue }".
6 dataType: 'json ',
7 success: function (result) {// callback function, result, Return Value
8 alert (result. d); // return value in the background after the ajax request is successfully popped up
9 if (result. d = "success "){
10
11 // successful event
12}
13}
14 });
C # You can use. aspx or. ashx (General transaction handler) to receive ajax requests. The web service in. aspx is used here.
1 [System. Web. Services. WebMethod ()]
2 public static string AddToTP (string tid, string uid, string score, string degree)
3 {
4 bool OK = DBHpler. executeNonQueryTwo ("Insert into trainingpeople (tid, uid, score, degree) Values ('" + tid + "', '" + uid + "', '"+ score +"', '"+ degree +"') "); // use the SQL help class to insert data to the trainingpeople table.
5 if (OK = true) {return "success";} // judge the return value of DBHpler. ExecuteNonQueryTwo
6 else {return "failed ";}
7}
2. when a user clicks the button, the ajax request returns the user-selected class personnel form and formatting the form. Here, the result is input for the class test. You can choose to enter the score directly or the level-2 system.
Front-end js Code
<Script language = "javascript" type = "text/javascript">
Function getPeople (){
Certificate ('{ssss'}.css ('visibility ', 'visable'); // loading Image Display
$. Ajax ({
Type: "POST", // WebService access request using Post
ContentType: "application/json", // WebService returns the Json type
Url: "ajax. aspx/GetPeople", // address and method name combination of WebService call ---- WsURL/method name
Data: '{unit: "' + $ ('# Select1 '). val () + '"}', // here the parameter to be passed is the value selected in the user drop-down box. Here, it is a Sub Unit selected by the user.
DataType: 'json ',
Success: function (data) {// callback function, result, Return Value
$ ('. Dd'). empty (); // clear the <div class = "dd">
$ ('. Dd'). append (data. d); // enter the ajax return value for <div class = "dd">
Upload ('{ssss'}.css ('visibility ', 'den den'); // loading image hiding
Var ss = $ ('# Select3 option: selected'). val ();
// Format the form based on the selected value
If (ss = "score "){
$ ('. Dd. scoretxt'). each (function (I ){
$ ('. Dd. scoretxt') [I]). append ('<input type = "text" class = "score"/> ');
});
}
Else {
$ ('. Dd. scoretxt'). each (function (I ){
$ ('. Dd. scoretxt ') [I]). append ('<select class = "score"> <option value = "qualified"> qualified </option> <option value = "unqualified"> unqualified </option> <option value = "not tested"> not tested </option> </select> ');
});
}
}
});
}
</Script>
Background code
In ajax. aspx
1 [System. Web. Services. WebMethod ()]
2 public static string GetPeople (string unit)
3 {
4 System. Threading. Thread. Sleep (500); // latency: 0.5 seconds
5 Page page = new Page ();
6 UserScoreInput ctl = (UserScoreInput) page. LoadControl ("~ \ Samples \ UserScoreInput. ascx "); // loads the Form Control.
7 ctl. unit = unit; // parameters passed to the Form Control
8
9 page. Controls. Add (ctl );
10 System. IO. StringWriter writer = new System. IO. StringWriter ();
11 HttpContext. Current. Server. Execute (page, writer, false );
12 string output = writer. ToString ();
13 writer. Close ();
14 return output;
15}
User Control
UserScoreInput. ascx code
1 <% @ Control Language = "C #" AutoEventWireup = "true" Inherits = "UserScoreInput" ClassName = "UserScoreInput" %>
2
3 <script runat = "server">
4 protected override void OnLoad (EventArgs e)
5 {
6
7
8 if (int. Parse (this. unit)> 1000)
9 sqlDsOrders. SelectCommand = "SELECT [ID], [name] FROM [personnel] WHERE unit = '" + this. unit + "% '";
10
11 base. OnLoad (e );
12}
13
14
15 </script>
16 // duplicate columns are used here
17 <asp: SqlDataSource ID = "sqlDsOrders" runat = "server" ConnectionString = "<% $ ConnectionStrings: ConnectionString %>">
18 </asp: SqlDataSource>
19 <asp: Repeater ID = "List" performanceid = "sqlDsOrders" runat = "server">
20 <HeaderTemplate>
21 <table style = "width: 80%">
22 <tr>
23 <th> name </th>
24 <th> score </th>
25 <th> name </th>
26 <th> score </th>
27 </tr>
28 </HeaderTemplate>
29 <ItemTemplate>
30 <tr>
31 <td style = "width: 25%">
32 <div class = "hh"> <% # Eval ("name") %> </div>
33 </td>
34 <td style = "width: 25%">
35 <div class = "scoretxt" name = '<% # Eval ("id") %>'>
36 </td>
37
38 </ItemTemplate>
39 <AlternatingItemTemplate>
40
41 <td style = "width: 25%">
42 <div class = "hh"> <% # Eval ("name") %> </div> </td>
43
44 <td style = "width: 25%">
45 <div class = "scoretxt" name = '<% # Eval ("id") %>'>
46 </td>
47 </tr>
48 </AlternatingItemTemplate>
49 <FooterTemplate>
50 </table>
51 </FooterTemplate>
52 </asp: Repeater>
UserScoreInput. ascx. cs code
1 public partial class UserScoreInput: UserControl
2 {
3 public string unit
4 {
5 get {return (string) ViewState ["unit"];}
6 set {ViewState ["unit"] = value ;}
7}
8
9}
If you have any shortcomings, please leave a message and learn together.
From oxzc