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:
- < asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
-
- < div id="show" runat="server">aaaa
- < /div>
-
- < asp:HiddenField ID="HiddenField1" runat="server" />
-
- < input type="button" value="1111" onclick="myFun()" id="Button2" />
- < asp:Button ID="Button1" runat="server" Text="getValue" OnClick="Button1_Click" />
-
Step 3: js
- < script>
- function myFun()
- {
- PageMethods.GetDate('a',myCallBack)
- }
-
- function myCallBack(result)
- {
- var di = document.getElementById("HiddenField1");
- di.value=result;
-
- var di = document.getElementById("show");
- di.innerHTML=result;
-
-
- }
-
- < /script>
-
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.
- [System.Web.Services.WebMethod]
- public static DateTime GetDate(string a)
- {
-
- return DateTime.Now;
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- DataTable dt = (DataTable)this.DataList1.DataSource;
- Response.Write(dt.Rows.Count);
- }
-
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
- < form id="form1" runat="server">
-
-
- < div id="show">
-
-
- < /div>
- < input type="button" onclick="CallServer()" value="CallServer">< /input>
-
Step 4,
Write js
- < script type="text/javascript">
- function CallServer()
- {
- var product = "1";
- < %= ClientScript.GetCallbackEventReference(this, "product", "ReceiveServerData",null)%>;
- }
-
- function ReceiveServerData(rValue)
- {
- alert(rValue);
- var di = document.getElementById("show");
- di.innerHTML=rValue;
- }
- < /script>
Step 5,
Background code
Declared variable: public string CallBackValue;
Interface Method:
- public string GetCallbackResult()
- {
- return CallBackValue + ",ok";
-
- }
-
- public void RaiseCallbackEvent(string eventArgument)
- {
- this.CallBackValue = eventArgument;
-
-
-
- }
-
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.
- Principles and Applications of ASP. NET1.1 Verification Code Generation
- Static File Processing: ASP. NET1.1 and ASP. NET2.0 are different.
- Run the windows program (ASP. NET1.1) in ASP. NET)
- Modify the template class for asp. net1.1 Development
- Connect Oracle9i with ASP. NET (ASP. NET1.1)