简体   繁体   English

打开jQuery对话框后防止回发

[英]Prevent Postback after opening jQuery dialog box

Page: 页:

<body>
 <form id="frmLogin" runat="server">  
  <asp:Button ID="btnClick" OnClientClick="openConfirmDialog();" OnClick="PopulateLabel"  runat="server"/>
  <div id="divDialog"></div>
  <asp:Label ID="lblText" runat="server"></asp:Label>
 </form>
</body>

JS JS

<script type="text/javascript">
 $(document).ready(function() {
 $("#divDialog").dialog({autoOpen: false,
                     buttons: { "Ok": function()
                                      { 
                                         $(this).dialog("close");
                                      },
                                "Cancel": function()
                                      { 
                                          $(this).dialog("close");
                                      }
                              } 
                   });
 });

 function openConfirmDialog()
 {
  $("#divDialog").dialog("open");
 }

C# C#

protected void Page_Load(object sender, EventArgs e)
 {
    lblText.Text = "";
 }

protected void PopulateLabel(object sender, EventArgs e)
 {
    lblText.Text = "Hello";
 }

This code opens me a dialog box with Ok and Cancel button but it do not wait for user activity and post the page immediately and the label gets populated. 此代码为我打开一个带有“确定”和“取消”按钮的对话框,但它不等待用户活动,而是立即发布页面,并填充标签。 I need to call the c# function based on user activity. 我需要根据用户活动调用c#函数。 If user clicks "Ok" label should get populated and if user clicks "Cancel" it should not call the c# function. 如果用户单击“确定”,则应填充标签,如果用户单击“取消”,则不应调用c#函数。 How do I achieve this? 我该如何实现?

First, to prevent the page from immediately posting back to the server, you need to cancel the default behavior of the click event by returning false from your handler: 首先,为防止页面立即回发到服务器,您需要通过从处理程序返回false来取消click事件的默认行为:

<asp:Button ID="btnClick" runat="server" OnClick="PopulateLabel"
    OnClientClick="openConfirmDialog(); return false;" />

Next, you need to perform the postback yourself when your Ok button is clicked: 接下来,您需要在单击“ Ok按钮时自行执行回发:

$("#divDialog").dialog({
    autoOpen: false,
    buttons: {
        "Ok": function() { 
            $(this).dialog("close");
            __doPostBack("btnClick", "");
        },
        "Cancel": function() { 
            $(this).dialog("close");
        }
    }
});

Note that the first argument to __doPostBack() is the name of the control (its UniqueID in ASP.NET terminology). 请注意, __doPostBack()的第一个参数是控件的名称 (ASP.NET术语中的UniqueID )。 Since the button is a direct child of the <form> element, we can hardcode its id in the __doPostBack() call, but things will get more complicated if it resides in a container hierarchy. 由于按钮是<form>元素的直接子元素,因此我们可以在__doPostBack()调用中对其id进行硬编码,但是如果它位于容器层次结构中,事情将会变得更加复杂。 In that case, you can use ClientScript.GetPostBackEventReference() to generate the appropriate call to __doPostBack() . 在这种情况下,可以使用ClientScript.GetPostBackEventReference()生成对__doPostBack()的适当调用。

EDIT: Since your page does not contain any postback-enabled control, __doPostBack() won't be defined on the client side. 编辑:由于您的页面不包含任何启用回发的控件,因此不会在客户端上定义__doPostBack() To work around that problem, you can use a LinkButton control instead of a Button control. 要变通解决此问题,您可以使用LinkBut​​ton控件而不是Button控件。

添加了另一个按钮,并使用jQuery click()事件触发新按钮的click事件,这又将触发C#中的相应事件处理程序

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

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