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?

Brack
  • 312
  • 2
  • 13
  • How do you implement editors validation? – Vladimir Apr 19 '18 at 09:49
  • I added how I implemented my validators. Not sure if you were asking for something else. – Brack Apr 19 '18 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? – Vladimir Apr 19 '18 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 – Brack Apr 19 '18 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: https://stackoverflow.com/questions/187482/how-can-i-use-the-button-tag-with-asp-net – Brack Apr 19 '18 at 12:42

2 Answers2

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); } }

Vladimir
  • 808
  • 5
  • 8
  • I tried adding this code to the class for my page, however it didn't seem to have any effect on my issue. – Brack Apr 19 '18 at 12:09
0

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?

Brack
  • 312
  • 2
  • 13