jquery validation for specific button onclick - asp.net

i am using jquery client side validation in asp.net.
the problem i am facing now is that whenever i click any button in a page it causes validation rather than for specific button alone.
how to resolve this .?
thank you

Suppose you have two buttons SubmitButton and CancelButton and you do not want to CancelButton to fire validation plugin, just give CssClass="cancel" for CancelButton
<asp:Button ID="CancelButton" runat="server"
Text="Return to List"
CssClass="cancel" />

You want to use event.preventDefault().
http://api.jquery.com/event.preventDefault/
$("button").click(function(event) {
event.preventDefault();
// do stuff here
});

Related

disabling asp button onclick prevents event from firing

I'm trying to disable an asp button when it is clicked on the client side.
<asp:Button runat="server" ID="btnSubmit" Text="Save" class="Button" OnClick="btnSubmit_Click" CausesValidation="false"></asp:Button>
<script type="text/javascript">
$("input[type=submit],button").click(function (){$(this).attr("disabled", "disabled");});
</script>
when the button is clicked it does post back to the page_load , but it won't go to the btnSubmit_Click method. If I remove the jquery that disables the button it makes it from the page_load to the btnSubmit_Click method.
It seems when the button is disabled from the jquery it is no longer able to wire up to its event. Anyone have any ideas how I can disable the button client side, and still make it to the specific onclick event method?
This is resolved (Yuriy answered), the issue was that I needed to put the disabled code within the form.submit client side code. If not the disabled button was not firing its click event, yet it was still posting back, just not going to the onclick method.
solution:
$('#Form1').submit(function(){ $("input[type=submit],button").click(function (){$(this).attr("disabled", "disabled");});

asp button and modal

So i have this button:
<asp:Button ID="Buttonid" runat="server" Text="View" BorderStyle="None" OnClick="Button_click_event" data-toggle="modal" data-target="#myModal"></asp:Button>
but then clicking the button will not toggle the OnClick Event, it will just show the modal.
But if i remove the data-toggle="modal" data-target="#myModal". The onclick event will work, but obviously the modal will not popup anymore.
Help please. Thanks
The script that is controlling your model more than likely stops the regular functions of the button so will stop it posting back which will stop the event handler from being fired.
To get both client side and server side to fire you'll have to post back and then display the model when the page reloads.

Preventing Double Clicking with Server Side Validation

I am trying to prevent users from double clicking on the submit button for a page I'm updating. The problem is that there is server side validation that has to be done to determine if the user has to do any updates before we can move on. I currently have the following code on my submit event for my form.
<asp:Button ID="SubmitPayment" runat="server" OnClientClick="this.disabled='true';" UseSubmitBehavior="false" OnClick="SubmitPayment_Click" Text="Submit" />
Where this becomes an issue is where the page does not pass validation. In this instance everything I have tried to enable the button has failed. It's hard for them to resubmit corrected information with the submit button disabled. I have tried both of the following methods at the end of my validation to re-enable it.
SubmitPayment.Enabled = true;
SubmitPayment.Attributes.Add("disabled", "false);
What must I do to re-enable this control? This is in Visual Studio 2005. Thanks in advance.
-edit-
The approach that I used was as follows. I added an additional decoy button that was disabled and had the display css property set to none. I then used javascript that would hide the submit button and show the decoy button in the OnClientClick event. Finally, the end of the server side method would update the css properties on the buttons to hide and show them afterwards.
Code segments are as follows. Thanks again for the help
function PreventDoubleClick()
{
var sp = document.getElementById("SubmitPayment");
var db = document.getElementById("DecoyButton");
sp.style.display = "none";
db.style.display = "inline";
}
<asp:Button ID="SubmitPayment" runat="server" OnClientClick="javascript:PreventDoubleClick();" UseSubmitBehavior="false" OnClick="SubmitPayment_Click" Text="Submit" />
<asp:Button ID="DecoyButton" Enabled="false" runat="server" style="display:none;" Text="Please Wait..."/>
DecoyButton.Style.Add("display", "none");
SubmitPayment.Style.Add("display", "inline");
I have had a similar problem before and the best solution I came up with was to hide the button and replace it with a please wait (or validating) message. If it fails validation you can then show the button again

asp.net and jquery dialog

does anyone know how to use jquery modal dialog in asp.net pages?
http://jqueryui.com/demos/dialog/
I've tried creating a simple aspx page, pasted code from the example (http://jqueryui.com/demos/dialog/), it almost works. The dialog briefly shows up and then disappears. It seems that the page is doing a postback. I'm not sure how to stop it, so that the modal dialog stays up.
I've been using jqModal for modal dialogs. I've set it up to use a standard HTML input button to trigger the dialog, and then you can put a regular asp.net button or other controls inside of the hidden div.
Hope this helps.
I assume you have something like:
<asp:Button ID="popupButton" OnClientClick="showModalPopup();" Text="Click me" runat="server" />
I would try adding "return false;" to the OnClientClick property of your Button:
<asp:Button ID="popupButton" OnClientClick="showModalPopup(); return false;" Text="Click me" runat="server" />
Or if you don't need to access the button in code-behind, simply use a standard HTML button and set it's onclick event attribute:
<button onclick="showModalPopup();">Click me</button>

ASP.NET CheckBox disabling postback with javascript

I'm trying to wire up a CheckBox to handle an event when check/unchecked. If the user has JavaScript enabled, use that, otherwise use a postback.
Here is my code:
<asp:CheckBox ID="ApplicationInProcessCheckBox" runat="server"
Text="Application In Process" AutoPostBack="true"
oncheckedchanged="ApplicationInProcessCheckBox_CheckedChanged"
onclick="return false;" />
The return false in the javascript onclick event is disabling the postback. However, it also won't let the box check or uncheck. (I have more code to add to the javascript event... I just want to get the concept working first).
What am I doing wrong?
I think we can't post back on clicking checkbox without Javascript enabled.

Resources