asp.net USES the ajax script to fetch data from master pages

  • 2020-05-10 18:01:04
  • OfStack

The method is as follows:
1. Drag ScriptManager into the page. To make it easy to use the Ajax script. Placed in other client controls at the same time to trigger the NetPost method. Client controls are not listed here.
 
<asp:ScriptManager ID="smMaster" runat="server" ScriptMode="Auto" EnablePageMethods="true"> 
</asp:ScriptManager> 

2. Write js in Master source code
 
function NetPost() { 
// Create an instance  
var wRequest = new Sys.Net.WebRequest(); 
// Set the request URL 
wRequest.set_url("../CheckState/CheckNetState.aspx"); 
// Define the parameters  
var body = "ip=192.168.1.1"; 
// Set the parameters  
wRequest.set_body(body); 
// Set request mode  
wRequest.set_httpVerb("POST"); 
// Request completion handler  
wRequest.add_completed(PostNet); 
// Perform the requested  
wRequest.invoke(); 
} 
function PostNet(exector, eventArgs) { 
// The state of the actuator E  : responseAvailable , aborted  or  timedOut .  
  // Only when the  responseAvailable  return  true  , the event handler can access other response information from the executor.  
if (exector.get_responseAvailable()) { 
  // Determine if the condition is normal  
if (exector.get_statusCode() == '200') { 
     // Get the return value  
var returnInfo = exector.get_responseData(); 
if (returnInfo == "0") { 
      // Display related content  
} 
} 
} 
} 

3. Description of add_completed() :
The registered event handler function must take two parameters:
1) a reference to the actuator that issued the network request. By accessing the actuator, you can check its status and retrieve the response data.
2) eventArgs parameter, which is set by the executor that raised the completion request event.
4. Write the request page
The request address is 1 page, without any HTML content, and only code is written in the Page_load method. The request page in this example is placed in the CheckState folder in the root directory with the name CheckNetState.aspx.
5. The request page code is as follows, mainly realizing Ping function.
 
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 parameter  
string ip = Request.Form["ip"]; 
int isConnect = PingServer(ip); 
// Enter the 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. After applying master page to other pages, test.
Sys. Net. WebRequest refer to: http: / / msdn microsoft. com/zh - cn/library/bb310979 aspx

Related articles: