Asp:Button - 如果OnClientClick = True |假

时间:2013-05-10 14:06:25

标签: c# html asp.net asp.net-mvc-3

<asp:Button ID="Invoice" runat="server" Text="Create Invoice" OnClientClick="CreateInvoice_Click()" OnClick="CreateInvoice_Click1"/>



        <script type="text/javascript" language="javascript">
                   function Create_Invoice() {
                    $("#dialog-confirm").dialog({
                        resizable: false,
                        height: 180,
                        modal: true,
                        buttons: {
                            Create: function () {
                                $(this).dialog("close");
                            },
                            Cancel: function () {
                              //code needed here
                                $(this).dialog("close");
                            }
                        }
                    });
                }
                </script>



    <p><span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span>Are you sure?</p>

因此,用户按下“创建发票”按钮,弹出窗口允许用户选择“创建”或“取消”。

如果用户点击“创建”或“取消”,则会在代码后面运行“CreateInvoice_Click”功能。我想知道什么(需要进入'取消'功能')如果点击取消,我怎么说忽略OnClick =“CreateInvoice_Click1”。?

感谢您的回复

4 个答案:

答案 0 :(得分:2)

如果你想阻止执行服务器端功能,你只需要在客户端函数中返回false。

function Create_Invoice() {
    $("#dialog-confirm").dialog({
        resizable: false,
        height: 180,
        modal: true,
        buttons: {
            Create: function () {
                $(this).dialog("close");
            },
            Cancel: function () {
              //code needed here
                $(this).dialog("close");
                return false;// that's all what you need
            }
        }
    });
}

答案 1 :(得分:0)

好像你正在尝试使用内置函数重新创建javascript所做的事情。

function confirmation() {
    var answer = confirm("Leave tizag.com?")
    if (answer){
        alert("Bye bye!")
        window.location = "http://www.google.com/";
    }
    else{
        alert("Thanks for sticking around!")
    }
}

取自http://www.tizag.com/javascriptT/javascriptconfirm.php

答案 2 :(得分:0)

你应该尝试使用javascript手动调用服务器端点击事件;
结帐CreateCancel按钮内的代码;

<script type="text/javascript">
         $('#test').on('click', function(e){
            e.preventDefault();
            $("#dialog-confirm").dialog({
              resizable: false,
              height: 180,
              modal: true,
              buttons: {
                 Create: function () {
                     $(this).dialog("close");
                     // manually calling serverside click event
                     $('#buttonHidden').click();
                 },
                  Cancel: function () {
                     //code needed here
                     $(this).dialog("close"); 
                      // don't call it manually here and thus it won't fire the serverside click event
                  }
              }
         });
      });
    </script>
    // your button here to call javascript
    <button id="test" runat="server">Create Invoice</button>
    // the hidden button just to hold the CreateInvoice_Click1 which is fired from fireClick()
    <asp:Button ID="buttonHidden" runat="server" Style="display: none" OnClick="CreateInvoice_Click1" />

你的代码背后;

 protected void CreateInvoice_Click1(Object sender, EventArgs e)
 {
    //your server side code

 }

答案 3 :(得分:0)

<asp:Button ID="Invoice" runat="server" Text="Create Invoice" OnClientClick="return Create_Invoice()" OnClick="CreateInvoice_Click1"/>

<script type="text/javascript" language="javascript">
function Create_Invoice() {
    $("#dialog-confirm").dialog({
        resizable: false,
        height: 180,
        modal: true,
        buttons: {
            Create: function () {
                $(this).dialog("close");
                return true;
            },
            Cancel: function () {
              //code needed here
                $(this).dialog("close");
                return false;
            }
        }
    });
}

<p id="dialog-confirm"><span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span>Are you sure?</p>

你的代码背后;

protected void CreateInvoice_Click1(Object sender, EventArgs e)
{
//your server side code   


}
相关问题