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 and relies on the internal viewstate. If the control value is modified with JS, The viewstate cannot be used, and these controls cannot be modified.ProgramSecurity issues are caused, 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, 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.
Examples,
The functions to be implemented in the following example are:
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.
1. 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 = "button#click"/>
Step 3: JS
}
</SCRIPT>
Step 4: backendCode
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)
{
Return datetime. now;
}
Protected void button#click (Object sender, eventargs E)
{
Datatable dt = (datatable) This. datalist1.datasource;
Response. Write (Dt. Rows. Count );
}
2. Use 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", "eseserverdata", null) %>;
}
Function compute eserverdata (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.