ASP.NET2.0 No flush client callback

Source: Internet
Author: User
Tags flush
asp.net| Client | refresh | no refresh

asp.net2.0 's client callback is a very exciting way to let us 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
The third: you want to write a JS function on the client, at the same time, the server will also pass the computed data to this function as
The parameters of this function.
Fourth one: Context specific what do you mean, I'm not sure.
Getcallbackeventrefernce sent to the customer, the end of the code is 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 manage the 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"%>
<title>server time</title>
<script language= "JavaScript" >

function GetServerTime ()
{
var message = ';
var context = ';

<%=sCallBackFunctionInvocation%>
}

function Showservertime (timemessage, context) {
Alert (' Now the time on the server is: \ n ' + timemessage ');
}
</script>
<body>
<form id= "mainform" runat= "Server" >
<input type= "button" value= "Get server-side time"/>
</form>
</body>


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"%>
<title>untitled page</title>
<script type= "Text/javascript" >
function test ()
{
var lb = document.getElementById ("Select1");
Take the dropdown box.
var con = lb.options[lb.selectedindex].text;
Get the text of the dropdown box you selected and then call it a calltheserver, a JS function that is output by the server side
Calltheserver (Con, "");
}
function Receiveserverdata (rValue)
{
results.innerhtml = RValue;
}
</script>
<body>
<form id= "Form1" runat= "Server" >
<div>
<select id= "Select1" >
<option value=1 selected= "selected" > Mouse apprentice </option>
<option value=2> Wuqi Master </option>
</select>
<br/>
<br/>
<input value= "return drop-down box text from server" type=button>
<br/>
<br/>
<span id= "Results" ></span>
<br/>
</div>
</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 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 type= "Text/javascript" ></script> tags to the script.
    }
 public string raisecallbackevent (String eventargument)
 {
  return Your choice is "+ eventargument;"
 }
}

The following are the results of the execution:


Third: The first two are <input type= "button" HTML control, then if the server button? Of course, you can also add the OnClick property of the server button in the background.
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 Master </option>
</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;




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.