Use Ajax scripts to retrieve data on the master page

Source: Internet
Author: User

Because the master page inherits from usercontrol, we cannot use jquey or Ajax pagemethods and other refreshing methods to retrieve data like normal pages. However, you can use the SYS. net. webrequest provided by ajax to solve this problem.

The method is as follows:

1,Drag scriptmanager to 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.

< ASP: scriptmanager ID = "Smmaster" Runat = "Server" Scriptmode = "Auto" Enablepagemethods = "True" >
</ ASP: scriptmanager >

2. Compile Js in master source code

Function Netpost (){
// Create an instance
VaRWrequest= New SYS. net. webrequest ();
// Set request URL
Wrequest. set_url ( "../Checkstate/checknetstate. aspx" );
// Define parameters
VaR Body =   " IP = 192.168.1.1 " ;
// Set parameters
Wrequest. set_body (body );
// Set Request Method
Wrequest. set_httpverb ( " Post " );
// Request completion Function
Wrequest. add_completed (PostNet );
// Execute request
Wrequest. Invoke ();
}
Function PostNet (exector, eventargs ){
// Executor status E: responseavailable, aborted, or timedout.
// Event processing only when responseavailable returns trueProgramTo access other response information of the executor.
If (Exector. get_responseavailable ()){
// Determine whether the status is normal
If (Exector. get_statuscode () =   ' 200 ' ){
// Returns the 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 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. It is only written in the page_load method.Code. 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.

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 Parameters
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.