0

I'm trying to use the HTML 5 button element to submit an ASP.NET form. I also want it to not trigger validation when clicked. This will submit the form, but will still cause validation to trigger:

<button runat="server"
CausesValidation="false"
type="submit"
name="myButton"
id="myButton">Press Here</button>

I'm specifically trying to not use HTML input or ASP:Button elements. They both will work, but don't allow HTML (images etc..) within the button text.

An example validation element on my page:

<asp:RequiredFieldValidator
ValidationGroup="myValGroup"
ControlToValidate="phone"
ID="phoneRequiredVal"
runat="server"
ErrorMessage="Phone is required."
Display="Dynamic"
CssClass="defaultErrorText"></asp:RequiredFieldValidator>

Any ideas?

5
  • How do you implement editors validation? Commented Apr 19, 2018 at 9:49
  • I added how I implemented my validators. Not sure if you were asking for something else. Commented Apr 19, 2018 at 11:34
  • I've checked your code, it does not cause form validation on the client side. The form is submitted correctly. However, I see the Event Validation error. Did you mean the Event validation issue? Commented Apr 19, 2018 at 11:51
  • For me, when I click the button, the page reloads (postback) and the validators all have their error messages displayed. I want the validators to ignore this button when it is clicked. When I change the <button> to an <input> it works as expected, however I'm trying to put HTML inside the button text and I can't do that with an input's value attribute. Commented Apr 19, 2018 at 12:02
  • My best guess at this point is that ASP.NET simply doesn't support the HTML 5 button element at all. The only reason the form gets submitted for me is that is it actually HTML/browser that does the form submission and posting of variables, and ASP just does work with them on the back end. Also why causesvalidation="false" isn't doing anything. If anyone can confirm that would be great. More information here: stackoverflow.com/questions/187482/… Commented Apr 19, 2018 at 12:42

2 Answers 2

1

Enter the LinkButton control. Looks like this is what I needed to submit the form, have HTML within the tags, and be able to halt validation:

<asp:LinkButton
runat="server"
CausesValidation="false"
CssClass="button fullWidth">Press Here</asp:LinkButton>

Found here: How do I make a form submit with a LinkButton?

Sign up to request clarification or add additional context in comments.

Comments

0

Although you use an HTML tag (<button>) as a server-side control (<button runat="server" ...>), the ASP.NET server does not expect this component as a postback or callback sender. As a result, the Event Validation error occurs on the server side during form submit.

To resolve this issue, register the button as a postback sender using ClientScriptManager.RegisterForEventValidation method:

public partial class _Default : System.Web.UI.Page { protected override void Render(HtmlTextWriter writer) { Page.ClientScript.RegisterForEventValidation("myButton"); base.Render(writer); } }

1 Comment

I tried adding this code to the class for my page, however it didn't seem to have any effect on my issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.