none
ClientScriptManager.GetCallbackEventReference中的context参数是什么啊 RRS feed

  • 问题


  • public string GetCallbackEventReference( Control control, string argument, string clientCallback, string context )中的context参数是什么意思啊。看了很多文章都是一笔带过,没有详解。哪位能给个详解啊!谢谢了!

    我是覃小华
    2011年12月22日 8:59

答案

  • 其實就是你要註冊到Client端的Script,從MSDN上這個範例來看就很清楚。

    protected void Page_Load(object sender, EventArgs e)
    {
    	// Define a StringBuilder to hold messages to output.
    	StringBuilder sb = new StringBuilder();
    
    	// Check if this is a postback.
    	sb.Append("No page postbacks have occurred.");
    	if (Page.IsPostBack)
    	{
    		sb.Append("A page postback has occurred.");
    	}
    
    	// Write out any messages.
    	MyLabel.Text = sb.ToString();
    
    	// Get a ClientScriptManager reference from the Page class.
    	ClientScriptManager cs = Page.ClientScript;
    
    	// Define one of the callback script's context.
    	// The callback script will be defined in a script block on the page.
    	StringBuilder context1 = new StringBuilder();
    	context1.Append("function ReceiveServerData1(arg, context)");
    	context1.Append("{");
    	context1.Append("Message1.innerText =  arg;");
    	context1.Append("value1 = arg;");
    	context1.Append("}");
    
    	// Define callback references.
    	String cbReference1 = cs.GetCallbackEventReference(this, "arg", 
    		"ReceiveServerData1", context1.ToString());
    	String cbReference2 = cs.GetCallbackEventReference("'" + 
    		Page.UniqueID + "'", "arg", "ReceiveServerData2", "", 
    		"ProcessCallBackError", false);
    	String callbackScript1 = "function CallTheServer1(arg, context) {" + 
    		cbReference1 + "; }";
    	String callbackScript2 = "function CallTheServer2(arg, context) {" + 
    		cbReference2 + "; }";
    
    	// Register script blocks will perform call to the server.
    	cs.RegisterClientScriptBlock(this.GetType(), "CallTheServer1", 
    		callbackScript1, true);
    	cs.RegisterClientScriptBlock(this.GetType(), "CallTheServer2", 
    		callbackScript2, true);
    
    }
    
    http://msdn.microsoft.com/zh-tw/library/ms153103(v=VS.100).aspx


    以上說明若有錯誤請指教,謝謝。
    http://www.dotblogs.com.tw/terrychuang/
    2011年12月22日 9:08

全部回复

  • 其實就是你要註冊到Client端的Script,從MSDN上這個範例來看就很清楚。

    protected void Page_Load(object sender, EventArgs e)
    {
    	// Define a StringBuilder to hold messages to output.
    	StringBuilder sb = new StringBuilder();
    
    	// Check if this is a postback.
    	sb.Append("No page postbacks have occurred.");
    	if (Page.IsPostBack)
    	{
    		sb.Append("A page postback has occurred.");
    	}
    
    	// Write out any messages.
    	MyLabel.Text = sb.ToString();
    
    	// Get a ClientScriptManager reference from the Page class.
    	ClientScriptManager cs = Page.ClientScript;
    
    	// Define one of the callback script's context.
    	// The callback script will be defined in a script block on the page.
    	StringBuilder context1 = new StringBuilder();
    	context1.Append("function ReceiveServerData1(arg, context)");
    	context1.Append("{");
    	context1.Append("Message1.innerText =  arg;");
    	context1.Append("value1 = arg;");
    	context1.Append("}");
    
    	// Define callback references.
    	String cbReference1 = cs.GetCallbackEventReference(this, "arg", 
    		"ReceiveServerData1", context1.ToString());
    	String cbReference2 = cs.GetCallbackEventReference("'" + 
    		Page.UniqueID + "'", "arg", "ReceiveServerData2", "", 
    		"ProcessCallBackError", false);
    	String callbackScript1 = "function CallTheServer1(arg, context) {" + 
    		cbReference1 + "; }";
    	String callbackScript2 = "function CallTheServer2(arg, context) {" + 
    		cbReference2 + "; }";
    
    	// Register script blocks will perform call to the server.
    	cs.RegisterClientScriptBlock(this.GetType(), "CallTheServer1", 
    		callbackScript1, true);
    	cs.RegisterClientScriptBlock(this.GetType(), "CallTheServer2", 
    		callbackScript2, true);
    
    }
    
    http://msdn.microsoft.com/zh-tw/library/ms153103(v=VS.100).aspx


    以上說明若有錯誤請指教,謝謝。
    http://www.dotblogs.com.tw/terrychuang/
    2011年12月22日 9:08
  • 这个我看过,但是解释也太多了,每一个解释都不一样,http://jackielin.cnblogs.com/archive/2005/11/27/callback.html他的解释,这是《Professional ASP.NET 4 in C# and VB》的解释Context Client-side variable that gets passed directly to the receiving client-side
    function. The context does not get passed to the server,你的解释比较和那另外一个博客的解释一致!


    我是覃小华
    2011年12月22日 9:18
  • 我自己验证了一下,这个context

    不但可以传送脚本,也利用传送一些其他非脚本的东西!比如说一个HTML标签!
    我是覃小华
    2011年12月22日 10:01