简体   繁体   English

通过部分回发加载时,jQuery停止在Asp.net控件中工作

[英]Jquery stops working in Asp.net control when loaded by a partial postback

in an ASP.net/C# application. 在ASP.net/C#应用程序中。 I am using the an update panel and a Placeholder inside it to dynamically load controls 我正在使用更新面板和其中的占位符来动态加载控件

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="LinkButton1" EventName="Click"/>
    </Triggers>
    <ContentTemplate>
        <asp:PlaceHolder ID="PlaceHolder1" runat="server">
        </asp:PlaceHolder>
    </ContentTemplate>
</asp:UpdatePanel>

When I click on the LinkButton1 I load a user control in the PlaceHolder: 当我单击LinkBut​​ton1时,我在PlaceHolder中加载了一个用户控件:

protected void LinkButton1_Click(object sender, EventArgs e)
{
    PlaceHolder1.Controls.Clear();
    MyControl C;
    C = (MyControl1 )LoadControl("Controls/MyControl.ascx");
    PlaceHolder1.Controls.Add(C);
}

All the control I have are loadded correctly. 我拥有的所有控件均已正确加载。

But the problem is this: 但是问题是这样的:

I have a control that uses Javascript and Jquery to create a drop down animation when I click on a div. 当单击div时,我有一个使用Javascript和Jquery创建下拉动画的控件。 This control works correctly when loaded on a page by a Normal PostBack But When I load it in the update panel using a partial post back, it loads but the javascript stops working (No more drop down animation and other stuff) 该控件在通过“普通回发”加载到页面上时可以正常工作,但是当我使用部分回发将其加载到更新面板中时,它可以加载,但是javascript停止工作(不再有下拉动画和其他内容)

How can I make them work when loading the control via a partial postback? 通过部分回发加载控件时,如何使它们工作?

I think using Jquery "Live" could be the answer your looking for, this will monitor changes in the DOM. 我认为使用Jquery“ Live”可能是您寻找的答案,这将监视DOM中的更改。

$("{YOURQUERY}").live("click", function(){ alert("Goodbye!"); }); $(“ {YOURQUERY}”)。live(“ click”,function(){alert(“ Goodbye!”);});

Look at the jquery documentation for further information. 查看jquery文档以获取更多信息。

I think your animation is being registered at a $(document).ready() event. 我认为您的动画正在$(document).ready()事件中注册。 Then it registers on the all divs, the specific animation. 然后,它在所有div上注册特定的动画。 If you do a postback to your updatepanel, then the document ready event isn't fired, so the animation is registered. 如果对更新面板执行回发操作,则不会触发文档就绪事件,因此会注册动画。 You should do it yourself again after a postback in an updatepanel. 在updatepanel中回发之后,您应该自己再次进行操作。

My guess is that your animation for drop downs is executed on ready of the dom. 我的猜测是下拉菜单的动画是在准备好dom之后执行的。 So your intial page load sets up the animation wherever it's needed on the drop downs. 因此,您的初始页面加载可在下拉菜单中所需的任何位置设置动画。

With a partial post back its only that section that gets loaded and so the animation is not set up. 如果有部分回发,则只有该部分会被加载,因此不会设置动画。 As it was not a part of the Dom on the initial ready. 由于它不是最初准备就绪的Dom的一部分。

You will need to trigger some javascript on return of that partial post back to run the same function you probably have to set up the animation. 您需要在该部分帖子返回时触发一些javascript,以运行可能必须设置动画的相同功能。

Edit 编辑

How to control which JavaScript gets run after UpdatePanel partial postback endRequest? 在UpdatePanel部分回发endRequest之后,如何控制运行哪个JavaScript?

Should help you execute the javascript after the update panel has returned. 返回更新面板后,应该可以帮助您执行javascript。

When you do a .bind() you only wire up the event for the elements present in the DOM at the given moment, ie when you execute the .bind(). 当您执行.bind()时,仅在给定时刻(即,执行.bind()时)为DOM中存在的元素连接事件。 The controls you load asynchronously via the partial postback are not present at document.ready which is why your events don't work. 通过部分回发异步加载的控件未出现在document.ready中,这就是事件无法正常工作的原因。

You should use live or delegate or, even better, if you are using jQuery 1.7 or later you should use the new on-function : 您应该使用live或委托,或者,如果使用的是jQuery 1.7或更高版本,甚至更好,则应该使用新的on-function

$(function(){
    $('placeholder-selector').on('click' , 'your-clickable-selector', function(){
        //do your animation here
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM