里面的UpdatePanel JavaScript代码 [英] JavaScript code inside UpdatePanel

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

问题描述

好:我只包含一个占位符一个aspx页面上有一个UpdatePanel

Ok: I've got an UpdatePanel on an aspx page that contains a single Placeholder.

这里面占位符取决于我追加选择用户控件之一。在某些外部条件(这是一个配置页)。

Inside this placeholder I'm appending one of a selection of usercontrols depending on certain external conditions (this is a configuration page).

在每个用户控件的存在bindUcEvents()的javascript结合的各种jQuery和JavaScript的事件按钮功能与用户控件内部验证。

In each of these usercontrols there is a bindUcEvents() javascript function that binds the various jQuery and javascript events to buttons and validators inside the usercontrol.

我遇到的问题是,用户控件的JavaScript是不被认可。通常情况下,当回来,但是,没有这个代码可以通过页面中发现的UpdatePanel的职位(我试着运行通过Firebug的控制台手动功能,但它告诉我它无法找到该功能)执行的UpdatePanel内的JavaScript。

The issue I'm having is that the usercontrol's javascript is not being recognised. Normally, javascript inside an updatepanel is executed when the updatepanel posts back, however none of this code can be found by the page (I've tried running the function manually via firebug's console, but it tells me it cannot find the function).

有没有人有什么建议吗?

Does anyone have any suggestions?

干杯,埃德。

编辑:

减少(但功能)例如:

标记:

<script src="/js/jquery-1.3.2.min.js"></script>
  <form id="form1" runat="server">
    <div>

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

    <asp:Button ID="Postback" runat="server" Text="Populate" OnClick="PopulatePlaceholder" />

    <asp:UpdatePanel ID="UpdateMe" runat="server">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Postback" EventName="Click" />
    </Triggers>
        <ContentTemplate>
            <asp:Literal ID="Code" runat="server" />
            <asp:PlaceHolder ID="PlaceMe" runat="server" />
        </ContentTemplate>
    </asp:UpdatePanel>
    </div>
 </form>



C#:

C#:

protected void PopulatePlaceholder(object sender, EventArgs e)
{
    Button button = new Button();
    button.ID = "Push";
    button.Text = "push";
    button.OnClientClick = "javascript:return false;";
    Code.Text = "<script type=\"text/javascript\"> function bindEvents() { $('#" + button.ClientID + "').click(function() { alert('hello'); }); }  bindEvents(); </script>";
    PlaceMe.Controls.Add(button);
}

您会看到该按钮不会poput警报消息,即使该代码出现在页面上。

You'll see that the button does not poput the alert message, even though the code is present on the page.

EDIT2:

好吧,只是要清楚,生产代码是显著比结合到一个文字只是一个单一的功能更复杂,并包含大量的

Ok, just to make it clear, the production code is significantly more complex than just a single function bound onto a literal, and contains a large number of

<%= Control.ClientID %>



的码位那将是非常困难的因素离成非特定的功能,并且毫无意义如他们每个只在一个地方(我们谈论的非常具体的验证和奇数的弹出触发+一些逻辑)使用。

bits of code that'll be very difficult to factor off into non-specific functions, and pointless as they're each only used in one place (we're talking very specific validation and the odd popout trigger + some logic).

推荐答案

摆脱立即控制并使用的ScriptManager注册脚本。你在做什么出于同样的原因

Get rid of that Literal control and use the ScriptManager to register your script. What you are doing doesn't work for the same reason

window.document.getElementById('someId').innerHtml = "<script type=\"text/javascript\">alert('hey');</script>";



不起作用。试试这个:

doesn't work. Try this:

protected void PopulatePlaceholder(object sender, EventArgs e)
{
    Button button = new Button();
    button.ID = "Push";

    button.Text = "push";
    button.OnClientClick = "return false;";


    string script = "function bindEvents() { $('#" + button.ClientID + "').click(function() { alert('hello'); }); }  bindEvents();";

  ScriptManager.RegisterClientScriptBlock(this.Page, typeof(SomeClass), Guid.NewGuid().ToString(), script, true);


  PlaceMe.Controls.Add(button);
} 

要的RegisterClientScriptBlock的参数可能需要改变,但你的想法。

The parameters to RegisterClientScriptBlock may need to change, but you get the idea.

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

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