为什么按钮控件需要单击两次?

发布于 2024-07-04 23:09:51 字数 531 浏览 8 评论 0 原文

我有一个使用 VB 和 Ajax 运行的 Web 应用程序。 我使用 updatepanels 来避免回发到服务器时出现令人恼火的“闪烁”。

我希望在 updatepanel 本身内定义一个按钮控件(尝试将其移到外部并出现一些灾难性错误,因此将其留在那里),这使得当前面板不可见,而同级面板可见。 此方法有效,但必须单击该按钮两次。 不是双击,而是单击一次而不是再次单击。

在设置断点时,我发现附加到按钮后面的代码实际上是在第一次单击时执行的,但面板没有按预期切换。 如果我单击相同的按钮或更糟糕的是,单击不同的按钮,则会出现第二个面板的预期行为。 然而,当单击第二个按钮时,会显示第三个面板,而第三个面板由于单击第二个按钮而变得可见。

我假设此行为是由于 updatepanel 及其 Ajax 性质造成的。 有没有办法避免第二次点击? 我是否滥用了更新面板? 我真的很想使用模态弹出窗口(就在 AjaxToolKit 中),但在回发数据时遇到问题,所以我选择了这种方法。 任何见解、帮助,甚至批评都将受到欢迎,因为这已经困扰我很长时间了。 谢谢

I've got a web application working using VB and Ajax. I'm using updatepanels to avoid the irritating "flicker" on postbacks to the server.

I would like to have a button control defined within the updatepanel itself (tried moving it outside and got some catastrophic error, so left it there) that makes the current panel not visible and a sibling panel visible. This works with the exception that the button must be clicked twice. Not double clicked, but clicked once than clicked again.

In setting breakpoints I discovered the code behind that's attached to the button is actually being executed on the first click, but the panels don't switch as expected. If I click the same button OR worse yet, a different button, the expected behavior of the second panel appearing occurs. However, with the second button being clicked there's an unwanted bonus of a third panel being displayed, the third panel being made visible due to the second button being clicked.

I'm assuming this behavior is due to the updatepanel and its Ajax nature. Is there a way to avoid the second click? Am I misusing the updatepanel? I really wanted to use a modal popup (right out of the AjaxToolKit) but had problems with posting back the data so I opted for this approach. Any insights, assistance, even criticism would be welcome as this has plagued me long enough. Thanks

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

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

发布评论

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

评论(8

伪心 2024-07-11 23:09:52

尝试在创建动态控件时为其指定一个 ID。
出于某种原因,.net 要求动态控件在此上下文中工作。

myControl.id="newID"

Try giving the dynamic control an ID when it is created.
For some reason this is required by .net for a dynamic control to work in this context.

myControl.id="newID"
长安忆 2024-07-11 23:09:52

正如其他人所说,更新面板仅更新其内容,这是使用它的主要好处之一。

Panel2 和 pnlPrvCmt 需要位于更新面板内,按钮单击方法才能正常工作。 另一种选择是将 Panel2 放入一个更新面板中,并将 pnlPrvCmt 放入第二个更新面板中。 然后,只要 UpdateMode=Always(默认情况下),任一更新面板内的任何控件都会导致两者刷新。

Like others have said an update panel only updates its contents, thats one of the main benefits of using it.

Panel2 and pnlPrvCmt need to be inside your update panel for your button click method to work. Another option would be to put Panel2 inside one update panel and pnlPrvCmt inside a second update panel. Then any control inside either update panel will cause both to refresh, as long as the UpdateMode=Always (which it is by default).

醉城メ夜风 2024-07-11 23:09:52

我发现这种情况发生在 2 种不同的场景下:

  1. 控件上未设置 ID。 ID 未包含在标记中,或者创建动态控件时未设置 ID。 ASP.Net 使用 ID 来跟踪操作。

  2. 嵌套的UpdatePanel。 场景:使用母版页时,您可能有一个包含在 UpdatePanel 中的内容占位符,以便页面内容中不需要 UpdatePanel。 然后,在开发页面时,您可能会按照习惯添加 UpdatePanel。

I have found this to occur under 2 different scenerios:

  1. No ID set on the control. Either the ID is left off of the markup or the the ID was not set when a dynamic control was created. ASP.Net uses the ID to track actions.

  2. Nested UpdatePanels. Scenerio: When using a Masterpage, you might have a content placeholder that you wrap in an UpdatePanel so that an UpdatePanel is not needed in the content on the page. Then, in developing your page you might, as a habit, add an UpdatePanel.

爺獨霸怡葒院 2024-07-11 23:09:51

如果您去掉 UpdatePanels,PostBacks 的工作是否会按预期进行? 您的 Page_Load 或链上的其他事件中的某些内容很可能在到达您的点击事件之前以某种方式“重置”。 可能是这样吗?

If you get rid of the UpdatePanels do things work as expected with PostBacks? Chances are something in your Page_Load or other event higher up the chain are "resetting" things in some way before it gets to your click event. Could this be the case?

情绪失控 2024-07-11 23:09:51

您的更新面板位于其他面板内。

应该反过来吗? 据我所知,只有更新面板中的控件才会通过 AJAX 调用进行更新。

Your update panel is sitting inside the other panels.

Should that be the other way around? AFAIK only controls within the update panel will get updated in via the AJAX call.

素年丶 2024-07-11 23:09:51

这是一个相当简单的解决方案。 (今天早上我遇到了同样的问题。)

UpdatePanel 无法渲染自身之外的内容。 因此,正如您所注意到的,更新正在发生,但您没有看到结果。

最简单的解决方案是强制完全回发。 您可以这样做:

protected override void OnInit(EventArgs e)
{
    var scriptManager = ScriptManager.GetCurrent(this);
    // or this.Page in a UserControl, etc.

    scriptManager.RegisterPostBackControl(someButton);
    scriptManager.RegisterPostBackControl(someOtherButton);
    // etc. for each control that needs to update something outside the UpdatePanel
}

这仍然允许 Ajax 在 UpdatePanel 中更新按钮本身(例如,将其状态更改为禁用或启用)。 仅当单击按钮时才会发生完整的回发。

Here's a fairly simple solution. (I was having the same problem this morning.)

The UpdatePanel can't render stuff outside itself. So, as you noticed, the updates are happening, but you're not seeing the result.

The easiest solution is to force a full postback. You can do that like this:

protected override void OnInit(EventArgs e)
{
    var scriptManager = ScriptManager.GetCurrent(this);
    // or this.Page in a UserControl, etc.

    scriptManager.RegisterPostBackControl(someButton);
    scriptManager.RegisterPostBackControl(someOtherButton);
    // etc. for each control that needs to update something outside the UpdatePanel
}

This still allows the buttons themselves to be updated in the UpdatePanel by Ajax (e.g. changing their state to disabled or enabled). The full postback only happens if the buttons are clicked.

喵星人汪星人 2024-07-11 23:09:51

我以前遇到过这个问题并解决了它,我只是不记得如何解决。 我会尝试找到我的旧代码并回复您。 想想看,您的脚本管理器中是否启用了 EnablePartialRendering? 也许尝试将两个容器包装在第三个面板中。

I have run into this before and resolved it, I just can't remember how. I will try to find my old code and get back to you. one thought, do you have EnablePartialRendering enabled in your scriptmanager? maybe try wrapping both containers in a third panel.

剧终人散尽 2024-07-11 23:09:51

我认为你的问题是方法执行后只有更新面板从服务器接收数据。 您尝试更改的面板位于更新面板之外,因此它不知道其属性已更改。

您要么需要进行整页回发,要么需要在更新面板中修改您想要修改的面板。

I think your problem is that only the update panel is receiving data from the server after the method executes. The panel your are trying to change is outside of the update panel so it does not know that its properties have changed.

You either need to do a full page postback or have the panel you wish to modify inside the update panel.

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