ASP.NET ICallbackEventHandler 和 ASP.net Ajax updatepanel

发布于 2024-11-15 14:00:44 字数 3091 浏览 1 评论 0原文

我想在一页上使用 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");
    }
}

上面的代码只是概念。我已经在一页上使用 ICallbackEventhandler 5 个控件,并且它们工作正常,现在我需要向使用 updatepanel 的页面添加一个新控件,它破坏了我所有其他“ICallbackEventhandler”控件。

i would like to use ajax UpdatePanel and ICallbackEventHandler on one page. Each will handle individual part of page, they are not related to each one.

If I remove UpdatePanel, ICallbackEventHandler is working. If I remove ICallbackEventHandler, update panel is working, but they are not working altogether :(

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>

And code behind:

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");
    }
}

The code above is just concept. I already have 5 control using ICallbackEventhandler on one page and they are working perfectly, now I need to add one new control to page which is using updatepanel, and it broke all of my other "ICallbackEventhandler" controls.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北恋 2024-11-22 14:00:44

几个小时后我找到了解决方案。

而不是

<script type="text/ecmascript">    BootX();</script>

我使用它

function pageLoad(sender, args) {setTimeout("BootX()", 25); }

并且它正在工作。解决方案非常简单:) UpdatePanel 使用回发,每次回发时都会调用 pageLoad 方法,这里我们调用回调函数。

After few hours I found solution.

Instead of

<script type="text/ecmascript">    BootX();</script>

I used

function pageLoad(sender, args) {setTimeout("BootX()", 25); }

And it's working. The solution was quite easy :) UpdatePanel uses Postback, everytime when postback is made pageLoad method is called and here we call callback func.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文