双击列表框时引发异步回发

发布于 2024-12-14 02:06:07 字数 1039 浏览 0 评论 0原文

我有一个 ASP.NET 列表框,lstActivities。要编辑列表中的项目,用户可以单击 lnkBut​​ton 或双击列表框。我通过以下方式实现此目的:

protected void Page_Load(object sender, EventArgs e) {
  if (IsPostBack) return;
  var refDblClick = ClientScript.GetPostBackEventReference(lnkButton, "dblClick");
  lstActivities.Attributes.Add("ondblclick", refDblClick);
}

protected override void Render(HtmlTextWriter writer)
{
  ClientScript.RegisterForEventValidation(lnkButton.UniqueID, "dblClick");
  base.Render(writer);
}

我想更改此设置,以便使用 AJAX 异步回发。目前,列表框和按钮位于 UpdatePanel 中,因此单击按钮时会发生异步回发。但是当双击列表框时,会发生完整的回发。

<asp:UpdatePanel ID="up" UpdateMode="Conditional" ChildrenAsTriggers="true"
 runat="server">
  <ContentTemplate>
    <asp:ListBox ID="lstActivities" runat="server"></asp:ListBox>
    <asp:LinkButton ID="lnkButton" runat="server" OnClick="lnkButton_Click">
     Edit</asp:LinkButton>
  </ContentTemplate>
</asp:UpdatePanel>

如何让双击只刷新UpdatePanel?

I have an ASP.NET listbox, lstActivities. To edit an item in the list, users can either click lnkButton or double-click on the listbox. I achieve this with:

protected void Page_Load(object sender, EventArgs e) {
  if (IsPostBack) return;
  var refDblClick = ClientScript.GetPostBackEventReference(lnkButton, "dblClick");
  lstActivities.Attributes.Add("ondblclick", refDblClick);
}

protected override void Render(HtmlTextWriter writer)
{
  ClientScript.RegisterForEventValidation(lnkButton.UniqueID, "dblClick");
  base.Render(writer);
}

I would like to change this so that the postback is asynchronous, using AJAX. At the moment, the listbox and button are in an UpdatePanel so there is an async postback when the button is clicked. But when the listbox is double-clicked, a full postback occurs.

<asp:UpdatePanel ID="up" UpdateMode="Conditional" ChildrenAsTriggers="true"
 runat="server">
  <ContentTemplate>
    <asp:ListBox ID="lstActivities" runat="server"></asp:ListBox>
    <asp:LinkButton ID="lnkButton" runat="server" OnClick="lnkButton_Click">
     Edit</asp:LinkButton>
  </ContentTemplate>
</asp:UpdatePanel>

How can I make the double-click refresh only the UpdatePanel?

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

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

发布评论

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

评论(3

眼睛会笑 2024-12-21 02:06:07

有几件事可以尝试:

<asp:UpdatePanel ID="up" UpdateMode="Conditional" ChildrenAsTriggers="true"
 runat="server">
   <Triggers>
      <asp:AsyncPostBackTrigger ControlID="lstActivities" />
       <asp:AsyncPostBackTrigger ControlID="lnkButton" />
   </Triggers>
   .........
</asp:UpdatePanel>

或者

protected void Page_Load(object sender, EventArgs e) {
  if (IsPostBack) return;
  var refDblClick = ClientScript.GetPostBackEventReference(lnkButton, "dblClick");
  lstActivities.Attributes.Add("ondblclick", refDblClick);

  ScriptManager1.RegisterAsyncPostBackControl(lstActivities);
}

A few things to try:

<asp:UpdatePanel ID="up" UpdateMode="Conditional" ChildrenAsTriggers="true"
 runat="server">
   <Triggers>
      <asp:AsyncPostBackTrigger ControlID="lstActivities" />
       <asp:AsyncPostBackTrigger ControlID="lnkButton" />
   </Triggers>
   .........
</asp:UpdatePanel>

or

protected void Page_Load(object sender, EventArgs e) {
  if (IsPostBack) return;
  var refDblClick = ClientScript.GetPostBackEventReference(lnkButton, "dblClick");
  lstActivities.Attributes.Add("ondblclick", refDblClick);

  ScriptManager1.RegisterAsyncPostBackControl(lstActivities);
}
执笏见 2024-12-21 02:06:07

我只是使用您提供的代码进行了快速测试,并且在单击按钮和双击列表时确实得到了“部分回发”(因为缺乏更好的术语,因为 updatepanels 总是进行完整回发)。

如果您将该页面中的其他面板设置为 UpdateMode="Conditional",就像您对 UpdatePanel“up”所做的那样,则只有“up”内的元素才会更新。如果您没有在其他面板上指定更新模式,那么它们将始终在回发时更新,因为更新面板始终执行完整回发;他们真正做的是部分刷新页面。

我认为链接有关 UpdatePanel 的 MSDN 文档是非常有帮助的阅读。

I just a ran a quick test with the code you provided and I do get "partial postbacks" (for lack of a better term since updatepanels always do full postbacks) on both, the clicking of the button and the double clicking of the list.

If you set other panels in that page to UpdateMode="Conditional" as you are doing with your UpdatePanel "up", then only elements inside "up" will be updated. If you don't specify the update mode on the other panels, then they will always be updated on postback, because, again, update panels ALWAYS do full postbacks; what they really do is partial refreshes of the page.

Linking MSDN documentation regarding UpdatePanel as I think is a very helpful read.

忆依然 2024-12-21 02:06:07

我尝试了建议的解决方案,但没有运气。这是一个相当复杂的页面,有很多 UpdatePanel,因此很难解决确切的问题。

最后我选择了 jQuery:

$(document).ready(function () {
  $(document).delegate('#ctl00_body_lstActivities', 'dblclick', function () {
    eval($('#ctl00_body_lnkButton').attr('href'));
  });
});

I tried the solutions suggested, but with no luck. It's quite a complicated page with lots of UpdatePanels so hard to work out the exact problem.

In the end I went for jQuery:

$(document).ready(function () {
  $(document).delegate('#ctl00_body_lstActivities', 'dblclick', function () {
    eval($('#ctl00_body_lnkButton').attr('href'));
  });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文