asp.net2.0 implement no flush client callback

Source: Internet
Author: User
Tags flush implement client
asp.net| Client | refresh | Without refreshing the ASP.NET2.0 client callback is an exciting way to control what data is submitted to the server without submitting the entire page, and the server simply returns the data you need instead of sending it back to the entire page.

First of all, we have to say a very important method: Getcallbackeventrefernce. I write my understanding out, may be wrong, please point out, thank you very much!

GetCallbackEventReference first implements the RaiseCallbackEvent method that gives the client script the ability to pass parameters to the server side. Then return the value of the RaiseCallbackEvent method to a parameter that you registered in the Getcallbackeventrefernce method (which is actually a script you want to write on the client). Call getcallbackeventrefernce You must pass the client script to him two parameters, one is to pass to the RaiseCallbackEvent event value, one is the context.

The meaning of his argument is as follows:

First: a page or server control that implements the ICallbackEventHandler excuse, and writes this to the front page.

Second: Represents the value that you want to pass from the client to the server RaiseCallbackEvent method

Third: You want to write a JS function on the client, at the same time, the server will also pass the computed data to the function as the parameter of this function.

Fourth: Context specific what meaning I am not very clear getcallbackeventrefernce sent to the customer, the end of the code is like this:

Webform_docallback (' __page ', arg,receiveserverdata,context,null,false)

So what can we do to call him from the client? See the three-way method:

The first: Write a public string in the background and assign the value to him in the Page_Load: =page.clientscript.getcallbackeventreference (this, "message", " Showservertime "," context ") Note that this is page.clientscrip because he will return a Clientscriptmanager,clientscriptmanager client script. Then <%= the public background string%> in the onclick event of a button in the foreground. Do a little experiment code as follows:

Front desk servertime.aspx: In order to easily remove a lot of useless html

<%@ page language= "C #" codefile= "ServerTime.aspx.cs" inherits= "servertime_aspx"%>


Server Time







Background:

Using System;
Using System.Web.UI;

public partial class Servertime_aspx:page,icallbackeventhandler
{
Be sure to realize icallbackeventhandler excuses
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, the point button results are as follows:

The second approach: in the above method we have to bind the backend in the foreground, so what if not binding? We do this:

Directly to the getcallbackeventreference as a JS function in the implementation of content, and then the JS function to register to the client.

Front Testpage Code:

<%@ Page language= "C #" autoeventwireup= "true" codefile= "TestPage.aspx.cs" inherits= "Testpage"%>


Untitled Page




mouse apprentice Wuqi Baby Master
















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", "receiveserverdata", "context");
String Callbackscript;
Callbackscript = "function Calltheserver (arg,context)" + "{" + Cbreference + "};";
Page.ClientScript.RegisterStartupScript (this. GetType (), "ABCDEFG", Callbackscript, True);
The fourth parameter represents whether you want to automatically add <script to the script type= "Text/javascript" > </script> tag, of course.
}
public string RaiseCallbackEvent (String eventargument)
{
Return "You choose is" + eventargument;
}
}

The following are the results of the execution:

Third: The first two are
Front third.aspx Code:

<%@ Page language= "C #" autoeventwireup= "true" codefile= "Third.aspx.cs" inherits= "third"%>
<title> Untitled Page </title>
<body>
<form id= "Form1" runat= "Server"
<div>
<select id= "Select1"
<option selected= "selected" value=1> mouse Apprentice </option>
<option value=2> Wuqi Baby Master
</select>
<asp:button id= "Button1" runat= "Servers" 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>
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 you can't pass the parameter to him in JS again.
String str = Page.ClientScript.GetCallbackEventReference (this, "document.getElementById" (' Select1 '). _
Options[document.getelementbyid (' Select1 '). Selectedindex].text "," Re ", null);
return false to prevent submission of forms
BUTTON1.ATTRIBUTES.ADD ("onclick", str+ "; return false;");
}

#region ICallbackEventHandler Members
 
public string RaiseCallbackEvent (String eventargument)
{
if (eventargument = = "Mouse Apprentice")
{
Return "Mouse Apprentice: Life is like a mouse, not in the closet on the toilet!" ";
}
Else
{
Return "Wuqi Teacher: Self-confidence and self-improvement, optimistic upward";
}
}
#endregion
}

Tips, when you finish writing System.Web.UI.ICallbackEventHandler, move the mouse up, then the system will have a small chart, dot he will automatically write the RaiseCallbackEvent code, the effect is as follows;


The third one feels the worst!



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.