asp.net use AJAX scripts to fetch data using master Pages

Source: Internet
Author: User
Tags copy net return string client root directory
The method is as follows:
1, the page dragged into the ScriptManager. To facilitate the use of Ajax scripts. Placed at the same time in other client controls, used 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, in the master source code to write JS
Copy CodeThe code is as follows:
function Netpost () {
Create an instance
var wrequest = new Sys.Net.WebRequest ();
Set Request URL
Wrequest.set_url (".. /checkstate/checknetstate.aspx ");
Defining parameters
var BODY = "ip=192.168.1.1";
Setting parameters
Wrequest.set_body (body);
Set Request mode
Wrequest.set_httpverb ("POST");
Request Completion handler function
Wrequest.add_completed (PostNet);
Execute request
Wrequest.invoke ();
}
function PostNet (Exector, EventArgs) {
The state of the executor e:responseavailable, aborted, or timedout.
An event handler can access additional response information for the executor only if responseavailable returns True.
if (exector.get_responseavailable ()) {
Determine if the status is normal
if (exector.get_statuscode () = = ' 200 ') {
Get the return value
var returninfo = Exector.get_responsedata ();
if (Returninfo = = "0") {
Show related content
}
}
}
}

3, add_completed () Description:
The registered event handler function must accept two parameters:
1 A reference to the executor that issued the network request. By accessing the executor, you can check its status and retrieve response data.
2) The EventArgs parameter, which is set by the executor that raised the request event to completion.
4. Write the request page
The request address is a page with no HTML content, and only code is written in the Page_Load method. The request page In this example is placed under the CheckState folder in the root directory, with the name: Checknetstate.aspx.
5, the request page code is as follows, mainly realizes 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 ();
Get the parameters
string IP = request.form["IP"];
int isconnect = Pingserver (IP);
Enter 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, Unit: 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, the use of other pages to apply the master page, test.
Sys.Net.WebRequest Please refer to: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.