Jquery Ajax application learning and application

Source: Internet
Author: User

Let's start with some basic knowledge:
Syntax: $. ajax ({option })
Option indicates the parameter. In the form of key/value.
Common parameters: type: post/get.
Url: the address where the request is sent.
Data: {object: object} the data sent to the server, which must be in key/value format. You can set the processData attribute without converting the data format.
Success: function (msg) {} callback function after successful request.
Error: function (msg) {} callback function after the request fails.
DataType: Data Type returned by xml/html/script/json/jsonp.
Let's look at the instance:
1. Use Ajax to implement the refresh review function (after clicking review, the LinkButton is unavailable and changed to "reviewed ).
The interface is as follows:

The Repeater control is used for data display, and the review button is the LinkButton Server Control.

Page Html code:Copy codeThe Code is as follows: <body>
<Form id = "form1" runat = "server">
<Div>
<Table width = "100%" border = "0" cellspacing = "0" cellpadding = "0" class = "Main_List_Table"
Rules = "none">
<Tr class = "Main_List_Table_Tr1">
& Lt; td width = "5%" & gt;
Select
</Td>
& Lt; td width = "18%" & gt;
Ticket No.
</Td>
& Lt; td width = "15%" & gt;
Status
</Td>
& Lt; td width = "18%" & gt;
Store
</Td>
& Lt; td width = "18%" & gt;
Purchaser
</Td>
& Lt; td width = "13%" & gt;
RMB
</Td>
<Td>
Operation
</Td>
</Tr>
<Asp: Repeater ID = "rpt_Review" runat = "server">
<ItemTemplate>
<Tr onmouseover = "this. className = 'main _ List_Table_Tr_Over '" onmouseout = "this. className = 'main _ List_Table_Tr_Out'">
<Td>
<Input type = "checkbox" id = "OrderId _ <% # Eval (" OrderId ") %> "name =" OrderId "value =" <% # Eval ("OrderId") %> "/>
</Td>
<Td>
<% # Eval ("OrderId") %> </a>
</Td>
<Td>
<% # Eval ("oState"). ToString () %>
</Td>
<Td>
Shop 1
</Td>
<Td>
Zhang San
</Td>
<Td>
100.00
</Td>
<Td>
<Asp: LinkButton ID = "lbtn_Audit_eShop_Price" runat = "server"> review </asp: LinkButton>
</Td>
</Tr>
</ItemTemplate>
</Asp: Repeater>
</Table>
</Div>
<Div class = "padding_Top10">
<Label for = "sel_All" style = "cursor: pointer">
<Input type = "checkbox" name = "sel_All" id = "sel_All" onclick = "selectAll ('sel _ all', 'orderid '); "/> select all/reselect </label>
<Input type = "button" id = "btn_Batch_Review" onclick = "return Batch_Review ();" value = "batch Review"/>
</Div>
</Form>
</Body>

The implementation code is as follows:
A. script code:Copy codeThe Code is as follows: function lbtn_Audit_eShop_Price_Command (obj, orderId)
{
If (obj. innerHTML! = "Review") return false;
If (! Confirm ("are you sure you want to review? ")){
Return false;
}
$. Ajax ({
Type: "post", // Post sending Method
Url: "AjaxTest. aspx", // The request address is the address of this page.
Data: {OrderId: orderId, Action: "Single_Review"}, // the incoming data is the order number and Action
Success :( function (msg ){
If (msg! = "")
{
Var chkId = "OrderId _" + orderId;
Document. getElementById (chkId). disabled = "disabled ";
Obj. disabled = "disabled ";
Obj. innerHTML = "reviewed ";
}
})
});
Return false;
}

B. server code:
Determine the event execution based on Action.Copy codeThe Code is as follows: protected void Page_Load (object sender, EventArgs e)
{
Conn. Open ();
// Execute the action
String Action = CommFun. Get_Safe_Str (Request ["Action"]);
If (! IsPostBack)
{
If (Action = "")
{
Rpt_Pro_Order_List_Bind ();
}
Else if (Action = "Single_Review") // click to review
{
Lbtn_Audit_eShop_Price_Command ();
}
Else // click for batch Review
{
Btn_Batch_Review_eShop_Price ();
}
}
}

After the Repeater binds data, it adds the onclick attribute to the LinkButton to execute the page script code.Copy codeThe Code is as follows: protected void rpt_Pro_Order_List_Bind ()
{
......
This. rpt_Review.DataSource = Dt;
This. rpt_Review.DataBind ();
For (int I = 0; I <this. rpt_Review.Items.Count; I ++)
{
LinkButton lbtn_Audit_eShop_Price = (LinkButton) this. rpt_Review.Items [I]. FindControl ("lbtn_Audit_eShop_Price ");
If (Dt. Rows [I] ["Audit_eShop_Price"]. ToString () = "1 ")
{
Lbtn_Audit_eShop_Price.Enabled = false;
Lbtn_Audit_eShop_Price.Text = "reviewed ";
}
Else
{
Lbtn_Audit_eShop_Price.Enabled = true;
Lbtn_Audit_eShop_Price.Attributes.Add ("onclick ",
"Return lbtn_Audit_eShop_Price_Command (this, '" + Dt. Rows [I] ["OrderId"]. ToString () + "');");
}
}
}
[Code]
2. Use Ajax to implement batch review without refreshing.
Note: The control used here must be an Html control, otherwise it will cause sending back. However, the internal server control of Repeater can also be used.
Here, you only need to add an onclick attribute to input = "btton" and directly execute the Ajax script.
The script content is as follows:
[Code]
// Batch Review
Function Batch_Review ()
{
If (! CheckSelItem ("OrderId", "select an order! ") Return false;
If (! Confirm ("are you sure you want to review? ") Return false;
Var OrderIds = ""; // record all order numbers
Var elements = document. getElementsByName ("OrderId ");
For (var m = 0; m <elements. length; m ++ ){
If (m = elements. length-1)
{
OrderIds = OrderIds + elements [m]. value;
}
Else
{
OrderIds = OrderIds + elements [m]. value + ",";
}
}
Var orderIdArr = OrderIds. split (',');
Var newOrderIdStr = "";
Var j = 0; // record the number of selected orders
Var position = ""; // record the selected order location
For (var I = 0; I <orderIdArr. length; I ++)
{
Var chk_Id = "OrderId _" + orderIdArr [I];
If ($ _ Id (chk_Id). checked) // record the selected order
{
If (I = orderIdArr. length-1)
{
NewOrderIdStr + = orderIdArr [I];
Position + = I;
}
Else
{
NewOrderIdStr + = orderIdArr [I] + ",";
Position + = I + ",";
}
J ++;
}
}
NewOrderIdStr = RemoveRightComma (newOrderIdStr); // remove the order number after a comma
Position = RemoveRightComma (position); // remove the position after the comma at the end
$. Ajax ({
Type: "POST ",
Url: "AjaxTest. aspx ",
Data: {Order_Id_Arr: newOrderIdStr, Action: "Batch_Review "},
Success: function (msg ){
If (msg! = ""){
For (var k = 0; k <j; k ++)
{
Var newOrderIdArr = newOrderIdStr. split (',');
Var positionArr = position. split (',');
$ _ Id ("OrderId _" + newOrderIdArr [k]). disabled = "disabled ";
If (parseInt (positionArr [k]) <10)
{
$ _ Id ("rpt_Review_ctl0" + parseInt (positionArr [k]) + "_ lbtn_Audit_eShop_Price"). innerHTML = "reviewed ";
$ _ Id ("rpt_Review_ctl0" + parseInt (positionArr [k]) + "_ lbtn_Audit_eShop_Price"). disabled = "disabled ";
}
Else
{
$ _ Id ("rpt_Review_ctl" + parseInt (positionArr [k]) + "_ lbtn_Audit_eShop_Price"). innerHTML = "reviewed ";
$ _ Id ("rpt_Review_ctl" + parseInt (positionArr [k]) + "_ lbtn_Audit_eShop_Price"). disabled = "disabled ";
}
}
}
}
})
Return true;
}
// Remove the right comma
Function RemoveRightComma (str)
{
If (str = "") return;
Var I;
For (I = str. length-1; I> = 0; I --)
{
// CharAt (I) is a character at a certain position.
If (str. charAt (I )! = ",") Break;
}
// Truncate the string, substring (start, stop); The returned result does not contain the last one.
Str = str. substring (0, I + 1 );
Return str;
}

So far, the instance explanation is complete.
Last Review:
1. Single Review: When initializing the page, add the onclick attribute to the review button, execute the page script, and click audit. The page_load event in the background selects the event to be executed based on the Action.
2. Batch Review: add the onclick attribute to the batch review button to execute the Ajax script. The page_load event in the background also selects the event to be executed based on the Action. The batch review button must be an Html control.

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.