admin管理员组文章数量:1415476
I am working on ASP.NET 3.5 webform application. 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.
protected void btnSubmit_Click(object sender, EventArgs e)
{
....
Now I have additional requirement, provide messagebox i.e. 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 i.e. e.preventDefault() when user select no otherwise carry on call to btnSubmit_Click
now below code doesn't stop calling server code because I am calling e.preventDefault() at later stage of process flow.
is there any way I can achieve this as for pause server calling???
JQuery Code
$(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");
});
});
I am working on ASP.NET 3.5 webform application. 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.
protected void btnSubmit_Click(object sender, EventArgs e)
{
....
Now I have additional requirement, provide messagebox i.e. 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 i.e. e.preventDefault() when user select no otherwise carry on call to btnSubmit_Click
now below code doesn't stop calling server code because I am calling e.preventDefault() at later stage of process flow.
is there any way I can achieve this as for pause server calling???
JQuery Code
$(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");
});
});
Share
Improve this question
asked Oct 22, 2015 at 11:24
K.ZK.Z
5,07527 gold badges116 silver badges264 bronze badges
4
- Try using return false when you want to prevent the call to server – Adil Commented Oct 22, 2015 at 11:27
- Sure! but how can I achieve when I want to?? – K.Z Commented Oct 22, 2015 at 11:29
- That's a very innocent and cute question :P – Waqar Ahmed Commented Oct 22, 2015 at 11:30
- Post you data using jQuery AJAX then show your message on success callback. – Sirwan Afifi Commented Oct 22, 2015 at 11:30
1 Answer
Reset to default 3You can use the OnClientClick
property for server controls in asp.
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.
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.
If it was a simple validation in javascript, like a required field, or any expected value would be very simple.
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.
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:
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.
本文标签: cenable or disable epreventDefault of javascript for ASPNET webform buttonStack Overflow
版权声明:本文标题:c# - enable or disable e.preventDefault of javascript for ASP:NET webform button - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745177897a2646329.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论