Asp.net 回调机制

前台aspx:
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    
<title>CallBack Page</title>
    
<script type="text/javascript">
        
function GetNumber() {
            UseCallback();
        }

        
function GetRandNumberFromSer(TextBox1, context) {
            document.forms[
0].TextBox1.value = TextBox1;
        }
    
</script>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<input id="Button1" type="button" value="Get Rand Num" onclick="GetNumber()" /><br />
        
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    
</div>
    
    
</form>
</body>
</html>

 后台CS页面:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page ,System.Web.UI.ICallbackEventHandler
{
    
private string _callbackRestule = "";
    
protected void Page_Load(object sender, EventArgs e)
    {
        
string cbRefer = Page.ClientScript.GetCallbackEventReference(this"arg""GetRandNumberFromSer""context");
        
string cbScript = "function UseCallback(arg,context)" + "{" + cbRefer + ";" + "}";

        Page.ClientScript.RegisterClientScriptBlock(
this.GetType(), "UseCallback", cbScript, true);
    }

    
public void RaiseCallbackEvent(string eventArg)
    {
        Random rnd 
= new Random();
        _callbackRestule 
= rnd.Next().ToString();
    }

    
public string GetCallbackResult()
    {
        
return _callbackRestule;
    }
}

 

生成的浏览页面源码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
    CallBack Page
</title>
    
<script type="text/javascript">
        
function GetNumber() {
            UseCallback();
        }
        
function GetRandNumberFromSer(TextBox1, context) {
            document.forms[
0].TextBox1.value = TextBox1;
        }
    
</script>
</head>
<body>
    
<form name="form1" method="post" action="Default.aspx" id="form1">
<div>
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJMjgzMDgzOTgzZGSopwuMyHhFr03Z7y0kgY8Ughlkyg==" />
</div>

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
    theForm 
= document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
    
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value 
= eventTarget;
        theForm.__EVENTARGUMENT.value 
= eventArgument;
        theForm.submit();
    }
}
//]]>
</script>

<script src="/WebSite1/WebResource.axd?d=_CLnVr0GkJ8I80CCN-XxtA2&amp;t=633809228264971767" type="text/javascript"></script>

<script type="text/javascript">
//<![CDATA[
function UseCallback(arg,context){WebForm_DoCallback('__Page',arg,GetRandNumberFromSer,context,null,false);}//]]>
</script>
<div>
    
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAgKT8YSWCwLs0bLrBkZdKNW+68rz1y5Ky5Wg/oK9Jpwj" />

</div>
    
<div>
        
<input id="Button1" type="button" value="Get Rand Num" onclick="GetNumber()" /><br />
        
<input name="TextBox1" type="text" id="TextBox1" />
    
</div>

<script type="text/javascript">
//<![CDATA[
WebForm_InitCallback();
//]]>
</script>
</form>
</body>
</html>

 

 

posted @ 2009-06-19 17:43 TONYBINLJ 阅读(...) 评论(...) 编辑 收藏