ASP.Net - 里面的Javascript AJAX的UpdatePanel [英] ASP.Net - Javascript inside AJAX UpdatePanel

查看:181
本文介绍了ASP.Net - 里面的Javascript AJAX的UpdatePanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,从外部JavaScript文件一个UpdatePanel内部运行的JavaScript。我试图让一个颜色选择器中的ListView内工作。 ListView控件是一个UpdatePanel内。

I am running into an issue with running javascript from an external javascript file inside of an UpdatePanel. I am trying to get a color picker working inside of a ListView. The ListView is inside of an UpdatePanel.

我使用这个颜色选择器

下面是我已经把范围缩小到:

Here is what I have narrowed it down to:

  • 如果我用拾色器上的一个文本框之外的的UpdatePanel ,其工作完全正常通过所有回传。

  • If I use the color picker on a textbox outside of an UpdatePanel, it works perfectly fine through all postbacks.

如果我用拾色器上的一个文本框的里面的UpdatePanel ,它的工作原理,直到我做一个异步回发(点击编辑按钮在ListView)。一旦的UpdatePanel 做回发,文本框将不再显示颜色选择器中单击时。当文本框是无论是在 InsertTemplate则 EditItemTemplate中在ListView的发生同样现象。

If I use the color picker on a textbox inside of an UpdatePanel, it works, until I do an async postback(clicking on an "EDIT" button in the ListView). Once the UpdatePanel has done the postback, the textbox will no longer show the color picker when clicked. The same occurs when the textbox is in either the InsertItemTemplate or EditItemTemplate of the ListView.

如果您想复制它,只需下载的颜色选择器(它是免费的),然后将它添加到网页...

If you would like to replicate it, simply download the color picker(it's free), then add this to a webpage...

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

<div>
    <asp:UpdatePanel ID="panel1" runat="server">
        <ContentTemplate>
            <asp:TextBox runat="server" ID="textbox" CssClass="color" />
            <asp:Button ID="Button1" runat="server" Text="Button" />
        </ContentTemplate>
    </asp:UpdatePanel>
</div>

在页面加载后,颜色选择器工作正常。当您单击按钮(其中做了回发),颜色选择器将不再工作。

When the page loads, the color picker works fine. When you click on the button(which does a postback), the color picker will no longer work.

任何想法?

推荐答案

异步往返之后,任何启动脚本将不会运行,这可能是为什么它不AJAX回调后的工作。颜色选择器可能具有需要在页面加载要执行的功能。

After an asynchronous roundtrip, any startup scripts will not be run, which is likely why it doesn't work after the AJAX callback. The color picker likely has functions which need to be executed on page load.

我碰到的这个很多次,我写了一个小方法,在code-后面,这可以处理异步和非异步往返登记自己的脚本。这里的基本轮廓:

I've run into this so many times that I wrote a small method to register my scripts in the code-behind, which handles both async and non-async round trips. Here's the basic outline:

private void RegisterClientStartupScript(string scriptKey, string scriptText)
{
    ScriptManager sManager = ScriptManager.GetCurrent(this.Page);

    if (sManager != null && sManager.IsInAsyncPostBack)
    {
        //if a MS AJAX request, use the Scriptmanager class
        ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), scriptKey, scriptText, true);
    }
    else
    {
        //if a standard postback, use the standard ClientScript method
        scriptText = string.Concat("Sys.Application.add_load(function(){", scriptText, "});");
        this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), scriptKey, scriptText, true);
    }
}

我竟然烤到上述基页类,以便任何网页我正在与可拨打 this.RegisterClientStartupScript(...)。要做到这一点,只需创建一个基本页面类,并将它包括有(并确保大关保护不是私人的或继承的页面类将不能访问它)。

I actually baked the above into a base page class so that any page I'm working with can call this.RegisterClientStartupScript(...). To do that, simply create a base page class and include it there (making sure to mark protected not private or your inheriting page classes won't be able access it).

通过上面的code,我可以自信而不管该页面是否是做了回发或回调注册客户端脚本。意识到你正在使用外部脚本文件,你也许可以修改注册外部脚本,而不是内联上面的方法。咨询的ScriptManager 类的更多细节,因为有几个脚本注册方法...

With the above code, I can confidently register client scripts regardless of whether the page is doing a postback or callback. Realizing you are using external script files, you could probably modify the above method to register external scripts rather than inline. Consult the ScriptManager class for more details, as there are several script registering methods...

这篇关于ASP.Net - 里面的Javascript AJAX的UpdatePanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆