Form Validators firing on Umbraco - asp.net

We are developing a web application in Umbraco 4 and have come across an intermittent problem when posting data between pages. When a form submission is posted to a new page all of the validators are firing causing various unwanted results, we have tried various posting methods using different buttons with the same result. Has anyone else come across this issue?

The validation group is working but I got to the bottom of the issue.
One of the submit buttons on the page was a html submit, with no runat server, so the code-behind didn't know where the submit had come from, so all validations where fired, regardless of validation group

All postbacks events, unless otherwise specified, will cause all validators on a page to be triggered. Either...
A) Set the ValidationGroup property on each of the validators as well as the control which you do want to trigger validators to the same name.
<asp:RequiredFieldValidator ID="valName" runat="server" AssociatedControlID="txtName" ValidationGroup="AllRequired" />
<asp:TextBox ID="txtName" runat="server" />
<asp:Button ID="btnSubmit" runat="server" ValidationGroup="AllRequired" OnClick="..." />
<asp:Button ID="btnSkip" runat="server" OnClick="..." />
In this case, btnSkip won't trigger the validators.
B) Set the CausesValidation property on the control which you don't want to trigger validation to false.
<asp:Button ID="btnSubmit" runat="server" CausesValidation="false" OnClick="..." />

Related

ASP.NET Webforms OnClientClick without enter submission

I have the following command button:
<asp:Button Text="Insert Value" UseSubmitBehavior="false" runat="server" OnClientClick="return Confirmation.value('insert');" CommandName="Insert" />
I am using UseSubmitBehavior="false" to prevent ASP.Net page enter key causing post back
I do not want to listen to the enter keyCode via javascript because enter is used to submit non-webform elements not related to the form
Apparently, when using a Command and UseSubmitBehavior="false" then OnClientClick doesnt work. If I turn on submit behavior it works as expected but then hitting enter on the page automatically tries to click the button.
I prefer not to listen for the click event in Jquery or in javascript, and prefer a webform solution. Possible a better way of prevent enter from submitting the form or a way for OnClientClick to work properly with no submit behavior
You may need to use Panel and keep all your form inside Panel Tag and set the DefaultButton value of Panel to Button. Like Below:
<asp:Panel ID="Panel1" runat="server" DefaultButton="Button1">
// other form elements
<asp:Button Text="Insert Value" ID="Button1" runat="server" OnClientClick="return Confirmation.value('insert');" />
</asp:Panel>

Prevent webform postback onclick button

I have a webform with an asp button that when clicked, needs to perform a function in c#. The function does not affect anything on the page so I do not need a refresh of the page.Is there any way to prevent an asp button from doing a postback? I am not familiar at all with JavaScript so I need to perform this function in c#. Surely there is a way. I have researched but nothing works. CausesValidation="false"' does not work. UseSubmitBehavior=false does not work. And neither does setting the OnClientClick to return false. Keep in mind I am not using JavaScript. Anyone know how?
Add a ScriptManager in your Page first,
Then add an update panel which will have all the controls. This will prevent the entire page being posted back.
here goes an example
<asp:ScriptManager ID="MainScriptManager" runat="server" />
<asp:UpdatePanel ID="pnlHelloWorld" runat="server">
<ContentTemplate>
<asp:Label runat="server" ID="lblHelloWorld" Text="Click the button!" />
<asp:Button runat="server" ID="btnHelloWorld" OnClick="btnHelloWorld_Click" Text="Update label!" />
</ContentTemplate>
</asp:UpdatePanel>
If you do not want to monkey with the ScriptManager or the server side stuff, you can always use an HTML control as in:
<input type="button" id="myButton" value="Do Something" onclick="doSomeFunction(); return false;" />
OR
<button id="myButton" onclick="doSomeFunction(); return false;">Do Something</button>
I did discover that attaching the click event to a button element via jQuery will result in a postback (at least that's what happened to me), so I switched to the tried and true input element.

Hide validations on form post back in aspx page

I have put server side validations for each text box using <asp:RequiredFieldValidator/>. I have called ClearFields() method on page load that will clear all fields on the form when the button is clicked. The problem is that when the form gets posted and the fields are cleared, the validation message appears again. How to hide the validation messages on form post back. I am sorry, but its been years I have not coded in aspx and I can't find any solution online.
This is the textbox code:
<asp:TextBox runat="server" CssClass="form-control" placeholder="Your Name *" ID="name"/>
<asp:RequiredFieldValidator runat="server" ControlToValidate="name" ErrorMessage="Name seems empty" CssClass="help-block text-danger"></asp:RequiredFieldValidator>
This is the button code:
<asp:Button runat="server" class="btn btn-xl" Text="Send Message" ID="submit" OnClick="submit_Click" CausesValidation="false"/>
I guess you are getting the validation message due to button used for clearing the fields. Set CausesValidation property of button to false:-
<asp:Button ID="ClearButton" runat="server" CausesValidation="false" Text="Clear" />
Also, please note asp:RequiredFieldValidator works on client side i.e on the browser it is not a server side validation.
Update:
Since you are clearing your fields on click of button, you can clear in submit_Click method itself after sending the email instead of page load. Ideally you should have a separate button to clear the form though.

ASP.NET Button submits page and I can't stop it

I have a simple proof of concept web page. It uses one master page and one content page, the default one, default.aspx. I'm doing some client side debugging with alert boxes. I dragged an asp.net button onto the page and set CausesValidation = false and UseSubmitBehavior = false and yet when I click it the page submits.
What am I doing wrong?
Here is a design time code....
<asp:Button ID="Button1" runat="server" Text="Button" UseSubmitBehavior="False" CausesValidation="False" />
Here is Runtime render, wth is it putting in a PostBack?
<input type="button" name="ctl00$ContentPlaceHolder1$Button1" value="Button" onclick="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Button1','')" id="ContentPlaceHolder1_Button1" />
--Update--
Thanks Volkan Paksoy, that worked. For those who suggested HTML buttons, that worked too and that is what I used, but I was just curious why the ASP.NET button wouldn't work. It's something I should know and probably something I knew and forgot. Thanks for the help
If you need to use asp:Button and disable submit you can add OnclientClick function such as:
<asp:Button ID="Button1" runat="server"
CausesValidation="False"
OnClick="Button1_Click"
Text="Button"
UseSubmitBehavior="False"
OnClientClick="return false;" />
This should stop the postback. But you can simply use an input too of course. This is just one way of doing it.
If you want to create a button that does not submit the page/postback to the server, you could create one using raw HTML like so:
<input type="button" />

IE 8 - ASP.NET form not submitting when user presses enter key

I have a simple form written in asp.net/C# and when trying to hit enter while in the form's input box doesn't submit the form for some reason. I had implemented a fix for a previous bug where pressing enter would merely refresh the page without submitting the form data but now pressing enter just does nothing, the fix is below:
<div style="display: none">
<input type="text" name="hiddenText" />
</div>
anybody know about a fix for this or a workaround?
I'm assuming you have a button somewhere on your page, as well as an event handler for it.
Have you tried wrapping your form (with the button) inside a Panel control and setting the default button attribute?
i.e.
<asp:Panel id="pnlMyForm" runat="server" DefaultButton="btnMyButton">
<asp:textbox id="txtInput" runat="server" />
<asp:Button id="btnMyButton" text="Submit" runat="server" />
</asp:Panel>
You can specify a default button for a form, which means hitting enter on any input control will fire that button (i.e. target the submit button). I haven't heard of this not working in any specific browser. This should eliminate your need for a workaround/hack.
<form id="form1" runat="server">
<asp:Panel ID="pnlFormContents" runat="server" DefaultButton="btnSubmit">
<!-- add some input controls as needed -->
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"/>
</asp:Panel>
</form>
Hope this helps...
I don't remember the specifics of the rules, but most browsers have the capability of submitting forms when ENTER is pressed if conditions are met. I think it had to do with whether you had 1 or more-than-one field, or whether or not there was at least one submit button (even if you hide it). I've done it in a site I recently did, but I don't have the code handy, but I can tell you it works without any special scripting. Check this posting for more details:
http://manfred.dschini.org/2007/09/20/submit-form-on-enter-key/

Resources