Asp.net use the ajax script on the master page to retrieve data

Source: Internet
Author: User

The method is as follows:
1. Drag ScriptManager into the page. To facilitate the use of Ajax scripts. It is also placed in other client controls to trigger the NetPost method. Client controls are not listed here.
Copy codeThe Code is as follows:
<Asp: ScriptManager ID = "smMaster" runat = "server" ScriptMode = "Auto" EnablePageMethods = "true">
</Asp: ScriptManager>

2. Compile js in Master source code
Copy codeThe Code is as follows:
Function NetPost (){
// Create an instance
Var wRequest = new Sys. Net. WebRequest ();
// Set the request URL
WRequest. set_url ("../CheckState/CheckNetState. aspx ");
// Define parameters
Var body = "ip = 192.168.1.1 ";
// Set parameters
WRequest. set_body (body );
// SET THE REQUEST METHOD
WRequest. set_httpVerb ("POST ");
// Request Completion processing function
WRequest. add_completed (PostNet );
// Execute the request
WRequest. invoke ();
}
Function PostNet (exector, eventArgs ){
// Executor status E: responseAvailable, aborted, or timedOut.
// Only when responseAvailable returns true can the event handler access other response information of the executor.
If (exector. get_responseAvailable ()){
// Determine whether the status is normal
If (exector. get_statusCode () = '201312 '){
// Obtain the returned value
Var returnInfo = exector. get_responseData ();
If (returnInfo = "0 "){
// Display related content
}
}
}
}

3. add_completed () Description:
The registered event handler function must accept two parameters:
1) A reference to the executor that sends a network request. By accessing the actuator, you can check its status and retrieve response data.
2) eventArgs parameter, which is set by the executor that triggers the request event.
4. Compile the request page
The request address is a page with no HTML content. You can only write code in the Page_load method. In this example, the request page is placed under the CheckState folder in the root directory and named CheckNetState. aspx.
5. The request Page code is as follows to implement the Ping function.
Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Namespace Iaddtech. Environmental. Web. UI. CheckState
{
Public partial class CheckNetState: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
Response. Clear ();
// Obtain the Parameter
String ip = Request. Form ["ip"];
Int isConnect = PingServer (ip );
// Input Return Value
Response. Write (isConnect. ToString ());
Response. End ();
}
Private int PingServer (string ip)
{
System. Net. NetworkInformation. Ping p = new System. Net. NetworkInformation. Ping ();
System. Net. NetworkInformation. PingOptions options = new System. Net. NetworkInformation. PingOptions ();
Options. DontFragment = true;
String data = "t ";
Byte [] buffer = System. Text. Encoding. ASCII. GetBytes (data );
Int timeout = 1000; // Timeout time, in milliseconds
System. Net. NetworkInformation. PingReply reply = p. Send (ip, timeout, buffer, options );
If (reply. Status = System. Net. NetworkInformation. IPStatus. Success)
Return 0;
Else
Return 1;
}
}
}

6. Apply the master page to other pages and test the settings.
For details about Sys. Net. WebRequest, refer:Http://msdn.microsoft.com/zh-cn/library/bb310979.aspx

Related Article

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.