我想在一个页面上使用ajax、UpdatePanel和ICallbackEventHandler。每一个都会处理页面的各个部分,它们与每个页面都不相关。
如果我删除UpdatePanel,ICallbackEventHandler将正常工作。如果我删除ICallbackEventHandler,更新面板可以工作,但它们不能完全工作:(
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default"
EnableEventValidation="false" %>
<!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></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
</head>
<body>
<script type="text/ecmascript">
function BootX() {
XUpdate();
}
function XUpdate() {
X_CallServer("", "");
}
function X_ReceiveServerData(rValue) {
$("#a").html(rValue);
}
</script>
<form id="form1" runat="server">
<div id="a">
</div>
<asp:ScriptManager runat="server" ID="ScriptManager1" />
<div>
<asp:Timer ID="Timer1" runat="server" Interval="100" OnTick="Timer1_Tick">
</asp:Timer>
<asp:UpdatePanel runat="server" ID="UpdatePanel1" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList runat="server" ID="ddl">
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
<script type="text/ecmascript">
BootX();
</script>
</body>
</html>和代码背后:
public partial class _Default : System.Web.UI.Page, ICallbackEventHandler
{
protected void Page_Load(object sender, EventArgs e)
{
String cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "X_ReceiveServerData", "context", true);
String callbackScript = string.Format("function X_CallServer(arg, context)" + "{{ {0} ;}}", cbReference);
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "X_CallServer", callbackScript, true);
ScriptManager1.RegisterAsyncPostBackControl(Timer1);
}
private string result = "";
public void RaiseCallbackEvent(string eventArgument)
{
// pretending some time consuming work
for (int i = 0; i < 1000000; i++) for (int j = 0; j < 100; j++) ;
result = "aaaaaaaaaaaaaaaaaaaaaaaaaaaa";
}
public string GetCallbackResult()
{
return result;
}
protected void Timer1_Tick(object sender, EventArgs e)
{
UpdatePanel1.Update();
Timer1.Enabled = false;
// pretending some time consuming work
for (int i = 0; i < 1000000; i++) for (int j = 0; j < 1000; j++) ;
ddl.Items.Add("Item 1");
ddl.Items.Add("Item 2");
ddl.Items.Add("Item 3");
}
}上面的代码只是一个概念。我已经在一个页面上有5个使用ICallbackEventhandler的控件,它们工作得很好,现在我需要添加一个新控件到使用updatepanel的页面,它破坏了我所有其他的"ICallbackEventhandler“控件。
发布于 2011-06-16 04:36:35
几个小时后,我找到了解决方案。
而不是
<script type="text/ecmascript"> BootX();</script>我用过
function pageLoad(sender, args) {setTimeout("BootX()", 25); }而且它起作用了。解决方案很简单:) UpdatePanel使用回发,每次回发时都会调用pageLoad方法,这里我们调用回调函数。
https://stackoverflow.com/questions/6359181
复制相似问题