I haven't written anything for a long time. I have been busy releasing my own technical website recently. I hope my technical network can bring more help to your work. www.yy0808.com
Asp. the client callback of Net2.0 is an exciting method, which allows us to control what data to submit to the server without submitting the entire page, at the same time, the server only returns the data you need instead of sending back the whole page.
First, let's talk about a very important method: GetCallbackEventRefernce. It may be wrong for me to write down my understanding. Thank you very much!
GetCallbackEventReference first enables the client script to pass parameters to the RaiseCallbackEvent method on the server, then return the value of the RaiseCallBackEvent method to a parameter you have registered in the GetCallbackEventRefernce method (in fact, it is also a script you want to write on the client ). To call GetCallbackEventRefernce, you must pass two parameters from the client script. One is the value to be passed to the RaiseCallbackEvent event, and the other is the context.
Its Parameter meanings are as follows:
First, it implements a page or server control that ICallbackEventHandler can use. Writing this indicates that the page is not displayed.
Second: indicates the value of the RaiseCallbackEvent method you want to pass from the client to the server.
Third: You need to write a js function on the client. At the same time, the server will pass the calculated data to this function as the parameter of this function.
Fourth: What does context mean? I am not quite sure that the Code sent by GetCallbackEventRefernce to the client is as follows:
WebForm_DoCallback ('_ page', arg, ReceiveServerData, context, null, false)
So what can we do to call it from the client? See the third method:
First, write a public string in the background and assign it to: = Page in Page_Load. clientScript. getCallbackEventReference (this, "message", "ShowServerTime", "context"); note that this is Page. clientScrip, because it returns a ClientScriptManager, which manages all client scripts. Then, in The onclick event of a button on the front-end, <% = that public background string %>, make a small experiment Code as follows:
Front-end ServerTime. aspx: Remove a lot of useless html for convenience
<% @ Page language = "C #" CodeFile = "ServerTime. aspx. cs" Inherits = "ServerTime_aspx" %>
<Html>
<Head>
<Title> Server Time </title>
<Script language = "javascript">
Function GetServerTime ()
{
Var message = '';
Var context = '';
<% = SCallBackFunctionInvocation %>
}
Function ShowServerTime (timeMessage, context ){
Alert ('the time on the current server is \ n' + timeMessage );
}
</Script>
</Head>
<Body>
<Form id = "MainForm" runat = "server">
<Input type = "button" value = "Get server end time" onclick = "GetServerTime ();"/>
</Form>
</Body>
</Html>
Background:
Using System;
Using System. Web. UI;
Public partial class ServerTime_aspx: Page, ICallbackEventHandler
{
// The ICallbackEventHandler must be implemented.
Public string sCallBackFunctionInvocation;
Void Page_Load (object sender, System. EventArgs e)
{
SCallBackFunctionInvocation = Page. ClientScript. GetCallbackEventReference (this, "message", "ShowServerTime", "context ");
}
Public string RaiseCallbackEvent (string eventArgument)
{
Return DateTime. Now. ToString ();
}
}
Run. click the button and the result is as follows:
Method 2: In the above method, we must bind the background at the front end. What if it is not bound? We do this:
Directly regard GetCallbackEventReference as an implementation content in the js function, and then register this js function to the client.
Front-end TestPage code:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "TestPage. aspx. cs" Inherits = "TestPage" %>
<Html>
<Head>
<Title> Untitled Page </title>
<Script type = "text/javascript">
Function test ()
{
Var lb = document. getElementById ("Select1 ");
// Obtain the drop-down list
Var con = lb. options [lb. selectedIndex]. text;
// Get the text in the drop-down box you selected and call CallTheServer, which is a js function output by the server.
CallTheServer (con ,'');
}
Function compute eserverdata (rValue)
{
Results. innerHTML = rValue;
}
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Select id = "Select1">
<Option value = 1 selected = "selected"> rat apprentice </option>
<Option value = 2> Master Wu qiwa </option>
</Select>
<Br/>
<Br/>
<Input onclick = "test ()" value = "return the drop-down box text from the server" type = button>
<Br/>
<Br/>
<Span ID = "Results"> </span>
<Br/>
</Div>
</Form>
</Body>
</Html>
Background code:
Using System;
Using System. Data;
Using System. Configuration;
Using System. Collections;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Public partial class TestPage: System. Web. UI. Page, System. Web. UI. ICallbackEventHandler
{
Protected void Page_Load (object sender, EventArgs e)
{
String cbReference = Page. ClientScript. GetCallbackEventReference (this, "arg", "eseserverdata", "context ");
String callbackScript;
CallbackScript = "function CallTheServer (arg, context)" + "{" + cbReference + "};";
Page. ClientScript. RegisterStartupScript (this. GetType (), "abcdefg", callbackScript, true );
// The fourth parameter indicates whether to automatically add the <script type = "text/javascript"> </script> flag to the script.
}
Public String RaiseCallbackEvent (String eventArgument)
{
Return "what you choose is" + eventArgument;
}
}
The execution result is as follows:
Third: the first two are <input type = "button" html controls. What if they are server buttons? You can also add the onclick attribute of the server button in the background.
Front-end third. aspx code:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "third. aspx. cs" Inherits = "third" %>
<Html>
<Head>
<Title> Untitled Page </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Select id = "Select1">
<Option selected = "selected" value = 1> rat apprentice </option>
<Option value = 2> Master Wu qiwa </option>
</Select>
<Asp: Button ID = "Button1" runat = "server" Text = "this is a server Button"/> </div>
<Div id = "div1"/>
<Script type = "text/javascript">
Function Re (ret)
{
Document. getElementById ("div1"). innerHTML = ret;
Alert (ret );
}
</Script>
</Form>
</Body>
</Html>
Background code:
Using System;
Using System. Data;
Using System. Configuration;
Using System. Collections;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Public partial class third: System. Web. UI. Page, System. Web. UI. ICallbackEventHandler
{
Protected void Page_Load (object sender, EventArgs e)
{
// The fourth parameter is null, because it is impossible for you to transmit parameters to JavaScript.
String str = Page. ClientScript. GetCallbackEventReference (this, "document. getElementById ('select1 ')._
Options [document. getElementById ('select1'). selectedIndex]. text "," Re ", null );
// Return false is used to prevent the form from being submitted
Button1.Attributes. Add ("onclick", str + "; return false ;");
}
# Region ICallbackEventHandler Members
Public string RaiseCallbackEvent (string eventArgument)
{
If (eventArgument = "rat Apprentice ")
{
Return "rat Apprentice: Life is like a rat. If you are not in the warehouse, you are in the toilet! ";
}
Else
{
Return "Master Wu banwa: self-confidence and self-improvement, optimistic ";
}
}
# Endregion
}
TIPS: After you finish writing System. Web. UI. ICallbackEventHandler and move the mouse up, there will be a small chart in front of the System. Click it to automatically write the RaiseCallbackEvent code. The effect is as follows;
The third is the worst!
For more wonderful articles, please log on to the Technical website I recently made in http://www.yy0808.com. Thank you for your support.