Use AJAX in ASP. NET

Source: Internet
Author: User

Introduction to the methods used in AJAX in ASP. NET

In asp.net, it is not easy to use jquery and other libraries commonly used by php to implement ajax. Because the asp.net mechanism has been encapsulated, relying on the internal viewstate, if the control value is modified with js, it cannot be matched with its viewstate, and these controls cannot be modified, it will cause security problems to the program, and it is also troublesome to obtain the value in the background.

In addition, the asp.net controls also encapsulate html controls, and js operations are not so straightforward.

According to Surance http://www.fltek.com.cn/) research found that in asp.net, there are three ways to use ajax is relatively simple. It is regarded as a compensation solution of ms.

One is PageMethod, the other is ICallbackEventHandler, and the other is ajax control that comes with ms.

The following are examples of functions to be implemented:

There is a div and a button on the page. Click the button to call the background method to obtain a time, and then write the time into the div. Do not refresh the page

In addition, there is a background button. Click this button to get the saved value.

Method 1 in AJAX in ASP. NET: pagemehtodd

Step 1: Create an asp.net ajax website or create a common website and modify webconfig)

Step 2: Create a control on the page:

 
 
  1. < asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />  
  2.  
  3.  < div id="show" runat="server">aaaa  
  4.          < /div>  
  5.  
  6.   < asp:HiddenField ID="HiddenField1" runat="server" />  
  7.  
  8.   < input type="button" value="1111" onclick="myFun()" id="Button2" />  
  9.         < asp:Button ID="Button1" runat="server" Text="getValue" OnClick="Button1_Click" />  
  10.  

Step 3: js

 
 
  1. < script>  
  2.         function myFun()  
  3.         {  
  4.             PageMethods.GetDate('a',myCallBack)  
  5.         }  
  6.           
  7.         function myCallBack(result)  
  8.         {  
  9.             var di = document.getElementById("HiddenField1");  
  10.             di.value=result;  
  11.               
  12.  var di = document.getElementById("show");  
  13.             di.innerHTML=result;  
  14.  
  15.    
  16.         }  
  17.           
  18.     < /script>  
  19.  

Step 4: Background code

Note: This method must be a static method and must be written to the following features.

Therefore, this method cannot directly access the page value.

 
 
  1. [System.Web.Services.WebMethod]  
  2.     public static DateTime GetDate(string a)  
  3.     {  
  4.  
  5.         return DateTime.Now;  
  6.     }  
  7.     protected void Button1_Click(object sender, EventArgs e)  
  8.     {  
  9.          DataTable dt = (DataTable)this.DataList1.DataSource;  
  10.          Response.Write(dt.Rows.Count);  
  11.     }  
  12.  

Method 2 in ASP. NET Using AJAX: Using ICallbackEventHandler

The first step is the same as above.

Step 2: Page implementation Interface

Public partial class Default2: System. Web. UI. Page, ICallbackEventHandler

Step 3: Create a control

 
 
  1. < form id="form1" runat="server">  
  2.       
  3.       
  4.     < div id="show">  
  5.       
  6.  
  7.     < /div>  
  8.     < input type="button" onclick="CallServer()" value="CallServer">< /input>  
  9.  

Step 4,

Write js

 
 
  1. < script type="text/javascript">  
  2.       function CallServer()  
  3.      {  
  4.          var product = "1";  
  5.          < %= ClientScript.GetCallbackEventReference(this, "product", "ReceiveServerData",null)%>;  
  6.      }  
  7.       
  8.      function ReceiveServerData(rValue)  
  9.      {  
  10.         alert(rValue);  
  11.              var di = document.getElementById("show");  
  12.             di.innerHTML=rValue;  
  13.     }  
  14.  < /script>  

Step 5,

Background code

Declared variable: public string CallBackValue;

Interface Method:

 
 
  1. public string GetCallbackResult()  
  2.     {  
  3.         return CallBackValue + ",ok";  
  4.  
  5.     }  
  6.  
  7.     public void RaiseCallbackEvent(string eventArgument)  
  8.     {  
  9.                        this.CallBackValue = eventArgument;  
  10.                   
  11.  
  12.  
  13. }  
  14.  

Note: RaiseCallbackEvent is a practical method.

GetCallbackResult is the callback method after the action is executed.

You can modify the value of a widget.

First, execute the background callback method, and then execute the frontend js callback method.

You can use RenderControl and other classes to output the asp.net control as html

You can use switchargument in RaiseCallbackEvent to check where the message is sent to call different functions.

  1. Principles and Applications of ASP. NET1.1 Verification Code Generation
  2. Static File Processing: ASP. NET1.1 and ASP. NET2.0 are different.
  3. Run the windows program (ASP. NET1.1) in ASP. NET)
  4. Modify the template class for asp. net1.1 Development
  5. Connect Oracle9i with ASP. NET (ASP. NET1.1)

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.