Jquery + ASP.NET 更新面板

发布于 2024-11-09 02:27:10 字数 1032 浏览 0 评论 0原文

我在工作中一直忙于在整个自定义 CRM 中实施 JQuery。在这个过程中,我创建了一个带边框的面板,它的标题中有一个简单的按钮,可以根据当前状态折叠或展开内容。现在,为了记住面板的状态,我正在使用 jquery cookie 库,当提交页面时,该库会为该控件设置一个 cookie,然后当重新加载页面时,它会读取面板并将其重置为最后的状态。

问题一是让 JQuery 在提交页面时执行,因为 ASP.NET 接管了这个任务并且不会调用表单上的 Submit() 函数。为了克服这个问题,我发现 http://kenbrowning.blogspot .com/2009/01/supporting-jquerys-formsubmit-in-aspnet.html 似乎几乎完美地工作。

然而......经过进一步测试,我发现这将阻止任何本应在 UpdatePanel 内发生的回发,以执行完整的回发,而不是从面板进行异步回发。我意识到 UpdatePanel 现在有点过时了,但它们散布在我们的网站中,因此为了使用此控件,它必须与它们很好地配合。

那么,是否有办法确定是否发生了异步回发?基本上我希望能够做这样的事情:

if (IsAsyncPostback) {
    theForm.submit(); //this seems to still fire the async postback, but doesn't call the jquery which is setup to save the state..
} else {
    $(theForm).submit(); //this is from the link, which cause all postbacks to be full on postbacks, even if they are in an updatepanel
}

救命,我已经四处寻找但找不到任何有用的东西..当然我不能是唯一尝试过这个的人?

I've been busily implementing JQuery throughout our custom CRM at work. In the process I create a bordered panel, which has the a simple button in it's header which collapses or expands the contents depending on what it's current state is. Now to remember the state of the panel I'm using the jquery cookie library, which when the page is submitted sets a cookie for that control, and then when the page is reloaded it reads and resets the panel to it's last state.

Problem one was getting JQuery to execute on the submit of the page, because ASP.NET takes over this and wouldn't call the submit() functions on the form. To overcome this, I found http://kenbrowning.blogspot.com/2009/01/supporting-jquerys-formsubmit-in-aspnet.html which seemed to work almost flawlessly.

However... After further testing, I've discovered that this will prevent any postbacks that are meant to occur within an UpdatePanel, to do a full Postback, instead of Async one from the panel. I realise UpdatePanels are a little out-dated now, but they are littered throughout our site, so in order to use this control it will have to work nicely with them.

So, is there anyway of determining if an async postback has taken place?? Basically I want to be able to do something like this:

if (IsAsyncPostback) {
    theForm.submit(); //this seems to still fire the async postback, but doesn't call the jquery which is setup to save the state..
} else {
    $(theForm).submit(); //this is from the link, which cause all postbacks to be full on postbacks, even if they are in an updatepanel
}

Help, I've searched around and haven't been able to find anything useful.. Sure I can't be the only person who has tried this?

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

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

发布评论

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

评论(2

我要还你自由 2024-11-16 02:27:10

我会避免干扰 __doPostBack() 函数。

我认为您想要的是处理 PageRequestManager 的 beginRequestendRequest 事件。这将在每次 UpdatePanel 刷新后运行您的代码,例如:

var prm = Sys.WebForms.PageRequestManager.getInstance();

prm.add_endRequest(function() {
  if ($('#panelID').is(':hidden'))
    $.cookie('#panelID', 'true');
  else
    $.cookie('#panelID', 'false');
});

如果您确实想更改 __doPostBack(),最好使用这种方法:

var oldDoPostBack = window.__doPostBack;

window.__doPostBack = function(target, arg) {
  // Trigger a custom event, to track when this occurs.
  $(window).trigger('doPostBack');

  // Call the real __doPostBack() function;
  oldDoPostBack(target, arg);
}

然后,您可以绑定到该代码从任何源触发 __doPostBack() 时执行操作的自定义事件:

$(window).bind('doPostBack', function() {
  alert('__doPostBack is about to execute.');
});

这里的重要优点是您无需重新定义 __doPostBack(),因此您不太可能会打破 它。

不过,我建议坚持使用内置的 PageRequestManager 事件,除非您确实因其他原因需要捕获 __doPostBack() 事件。

I would avoid interfering with the __doPostBack() function.

I think what you want is to handle the PageRequestManager's beginRequest or endRequest events. This will run your code after every UpdatePanel refresh, for example:

var prm = Sys.WebForms.PageRequestManager.getInstance();

prm.add_endRequest(function() {
  if ($('#panelID').is(':hidden'))
    $.cookie('#panelID', 'true');
  else
    $.cookie('#panelID', 'false');
});

If you really wanted to change __doPostBack(), it would be better to do so with this sort of approach:

var oldDoPostBack = window.__doPostBack;

window.__doPostBack = function(target, arg) {
  // Trigger a custom event, to track when this occurs.
  $(window).trigger('doPostBack');

  // Call the real __doPostBack() function;
  oldDoPostBack(target, arg);
}

Then, you could bind to that custom event to take actions when __doPostBack() was triggerd from any source:

$(window).bind('doPostBack', function() {
  alert('__doPostBack is about to execute.');
});

The important advantage here is that you aren't redefining __doPostBack(), so there's less chance that you'll break it.

I'd recommend sticking with the built-in PageRequestManager events though, unless you really do need to catch __doPostBack() events for some other reason.

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