首页 新闻 会员 周边

关于ICallBackEventHandler的问题

0
悬赏园豆:100 [已解决问题] 解决于 2012-03-06 15:34

 我现在有一个页面,里面包含了两个UserControl

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
2
3 <%@ Register Src="WebUserControl1.ascx" TagName="WebUserControl1" TagPrefix="uc1" %>
4 <%@ Register Src="WebUserControl2.ascx" TagName="WebUserControl2" TagPrefix="uc2" %>
5 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
6 <html xmlns="http://www.w3.org/1999/xhtml">
7 <head runat="server">
8 <title></title>
9 </head>
10 <body>
11 <form id="form1" runat="server">
12 <uc1:WebUserControl1 ID="WebUserControl11" runat="server" />
13 <hr />
14 ------------------------------------------------------------------------------------
15 <uc2:WebUserControl2 ID="WebUserControl21" runat="server" />
16 </form>
17 </body>
18 </html>

 

这两个UserControl 都实现了ICallBackEventHandler接口

WebUserControl1 页面
 1 <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs"
2 Inherits="WebApplication1.WebUserControl1" %>
3 <div id="WebUserControl1Div">
4 </div>
5 <script>
6
7 function ReceiveServerData1(rValue) {
8 document.getElementById("WebUserControl1Div").innerHTML = rValue;
9 }
10 </script>

 

WebUserControl1 后台类
namespace WebApplication1
{
public partial class WebUserControl1 : System.Web.UI.UserControl, ICallbackEventHandler
{
protected void Page_Load(object sender, EventArgs e)
{
string script = this.Page.ClientScript.GetCallbackEventReference(this, "", "ReceiveServerData1", null);
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "callback0", string.Format("\n{0};\n", script), true);
}

public string GetCallbackResult()
{
return "WebUserControl1, OK";
}

public void RaiseCallbackEvent(string eventArgument)
{
}
}
}


WebUserControl2 页面
 1 <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl2.ascx.cs"
2 Inherits="WebApplication1.WebUserControl2" %>
3 <div id="WebUserControl2Div">
4 </div>
5 <script>
6
7 function ReceiveServerData2(rValue) {
8 document.getElementById("WebUserControl2Div").innerHTML = rValue;
9 }
10 </script>

 

WebUserControl2 后台类

 1 namespace WebApplication1
2 {
3 public partial class WebUserControl2 : System.Web.UI.UserControl, ICallbackEventHandler
4 {
5 protected void Page_Load(object sender, EventArgs e)
6 {
7 string script = this.Page.ClientScript.GetCallbackEventReference(this, "", "ReceiveServerData2", null);
8 this.Page.ClientScript.RegisterStartupScript(this.GetType(), "callback1", string.Format("\n{0};\n", script), true);
9 }
10
11 public string GetCallbackResult()
12 {
13 return "WebUserControl2 , ok!";
14 }
15
16 public void RaiseCallbackEvent(string eventArgument)
17 {
18 }
19 }
20 }

 

最后页面运行之后的结果。

也就是说只有WebUserControl2 才起作用

WebUserControl1页面上没有处理

实际上经过调试。请求都已经到服务器上去了。只不过只有一个回调函数接受到服务器上返回的值了..

 但是我在页面上任意删除一个 UserControl 都能够执行..

 

这是什么原因呢 ?

kech的主页 kech | 初学一级 | 园豆:12
提问于:2011-11-10 12:32
< >
分享
最佳答案
1

两个UserControl都放在Page页面中了,This指的是Page实例,都是this.Page.GetCallbackEventReference

收获园豆:100
爱研究源码的javaer | 小虾三级 |园豆:930 | 2011-11-15 21:58

哦。 对呢。这个我倒是没注意啊。谢谢指点..

kech | 园豆:12 (初学一级) | 2011-11-16 09:15

@kech: Page.GetCallbackEventReference,没有这个方法啊?

卓酷 | 园豆:65 (初学一级) | 2013-05-28 17:44
其他回答(2)
0

很明显每次只能调用一个啊。

悟行 | 园豆:12559 (专家六级) | 2011-11-10 13:30

 不能调用两个吗 ?

支持(0) 反对(0) kech | 园豆:12 (初学一级) | 2011-11-10 13:54

@Chenk: 能调用两个,不能同时运行两个。

支持(0) 反对(0) 悟行 | 园豆:12559 (专家六级) | 2011-11-10 13:59

@站在牛人的肩上: 

 你的意思是 用一个UserControl 实现ICallbackEventHandler 接口

    在客户端根据不同的参数来请求到 RaiseCallbackEvent 方法来区分吗 ?

支持(0) 反对(0) kech | 园豆:12 (初学一级) | 2011-11-10 14:06

@Chenk: 嗯

支持(0) 反对(0) 悟行 | 园豆:12559 (专家六级) | 2011-11-10 14:07
0

你传入this.Page.ClientScript.GetCallbackEventReference的第一个参数,都是this(也就是当前page),你试试传入不同的UserControl实例,而不是当前Page实例。

水牛刀刀 | 园豆:6350 (大侠五级) | 2011-11-10 15:44

 this 起始就是指的UserControl本身啊.

两个不同的UserControl 中的 this 意义也不一样啊. 

支持(0) 反对(0) kech | 园豆:12 (初学一级) | 2011-11-11 10:40

@Chenk: this是Page实例。不是UserControl的实例。

支持(0) 反对(0) 水牛刀刀 | 园豆:6350 (大侠五级) | 2011-11-11 10:49

@水牛刀刀: 

1 namespace WebApplication1
2 {
3 public partial class WebUserControl2 : System.Web.UI.UserControl, ICallbackEventHandler
4 {
5 protected void Page_Load(object sender, EventArgs e)
6 {
7 string script = this.Page.ClientScript.GetCallbackEventReference(this, "", "ReceiveServerData2", null);
8 this.Page.ClientScript.RegisterStartupScript(this.GetType(), "callback1", string.Format("\n{0};\n", script), true);
9 }
10
11 public string GetCallbackResult()
12 {
13 return "WebUserControl2 , ok!";
14 }
15
16 public void RaiseCallbackEvent(string eventArgument)
17 {
18 }
19 }
20 }
这个 this 是在 WebUserControl2  类中的呀...怎么会是Page的实例呢?
支持(0) 反对(0) kech | 园豆:12 (初学一级) | 2011-11-11 10:53
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册