简体   繁体   English

为ASP:.NET Webform按钮启用或禁用javascript的e.preventDefault

[英]enable or disable e.preventDefault of javascript for ASP:NET webform button

I am working on ASP.NET 3.5 webform application. 我正在研究ASP.NET 3.5 Webform应用程序。 I have submit button in webform 我在网络表单中有提交按钮

   <cc1:FlatButton ID="btnSubmit" runat="server" class="positive" Text="Submit Assessment" OnClick="btnSubmit_Click" RemoveDiv="True" />

which call OnClick="btnSubmit_Click code-behind. 后者在代码后调用OnClick =“ btnSubmit_Click。

protected void btnSubmit_Click(object sender, EventArgs e)
    {
         ....

Now I have additional requirement, provide messagebox ie alert which appear when user click on above submit button, and based on user option selection, form submit or not 现在,我还有其他要求,请提供消息框,即警报,当用户单击上方的“提交”按钮时出现,并根据用户选项的选择来决定是否提交表单

My question is how can I prevent to call code-behind btnSubmit_Click event based on user selection ieepreventDefault() when user select no otherwise carry on call to btnSubmit_Click 我的问题是,当用户选择否时,如何防止基于用户选择ieepreventDefault()的btnSubmit_Click事件背后的代码调用,否则继续调用btnSubmit_Click

now below code doesn't stop calling server code because I am calling e.preventDefault() at later stage of process flow. 现在下面的代码不会停止调用服务器代码,因为我在流程流的后期调用e.preventDefault()。

is there any way I can achieve this as for pause server calling??? 对于暂停服务器调用,有什么办法可以实现吗???

JQuery Code jQuery代码

 $(document).ready(function () {

        createDialogElements();

        $("#ctl00_ContentArea_btnSubmit").click(function (e) {

           $("#WindowMessageDialog").jqxWindow('open');

           $("#messageDialog_btn_no").click(function () {

               alert("answer is no");

               e.preventDefault();
           });

           $("#messageDialog_btn_yes").click(function () {

               alert("answer is yes");
           });
        });

You can use the OnClientClick property for server controls in asp.net. 您可以将OnClientClick属性用于asp.net中的服务器控件。

The problem is, when you click the submit button, the function defined in OnClientClick will be triggered and its result must be true of false, to continue or cancel the submit. 问题是,当您单击提交按钮时,将触发OnClientClick定义的函数,其结果必须为true或false,才能继续或取消提交。 In your example, you're opening a dialog and waiting for an user click, but the javascript will not stop for waiting that action from user. 在您的示例中,您正在打开一个对话框并等待用户单击,但是javascript不会停止等待用户的操作。

If it was a simple validation in javascript, like a required field, or any expected value would be very simple. 如果这是使用javascript进行的简单验证(例如必填字段),则任何期望值都将非常简单。

There are some solutions. 有一些解决方案。 The most simple (but not the better) I could think is to create a variable to control if your form can be submitted. 我能想到的最简单(但并非更好)是创建一个变量来控制是否可以提交表单。 When you click the submit, it will call a javascript function that open you dialog, and return false, so the form will not be submited. 当您单击提交时,它将调用一个javascript函数,该对话框将打开您的对话框,并返回false,因此将不提交该表单。

When the dialog closes, if the form is valid, it will submit the form. 对话框关闭时,如果表单有效,它将提交表单。

Try this, to set a client function on click: 尝试此操作,以在单击时设置客户端功能:

 <cc1:FlatButton ID="btnSubmit" runat="server" class="positive" Text="Submit Assessment" OnClick="btnSubmit_Click" OnClientClick="if (validForSubmit) return true; else  return validateForm();" RemoveDiv="True" />

And replace your jQuery code by this Javascript function: 并使用以下Javascript函数替换您的jQuery代码:

var validForSubmit = false;

function validateForm() {
    $("#WindowMessageDialog").jqxWindow('open');

    $("#messageDialog_btn_no").click(function () {
       alert("answer is no");
       return false;
    });

    $("#messageDialog_btn_yes").click(function () {
       alert("answer is yes");
       // set form as valid for submit
       validForSubmit = true;
       // fire the click, because the form is now valid
       $("#ctl00_ContentArea_btnSubmit").click();
       return true;
    });

    return false;
}

You may need to fix something, but this is the idea, hope it helps. 您可能需要修复某些问题,但这是个主意,希望对您有所帮助。

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

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