0

It looks very simple, but becoming increasingly frustrating. I am working with a ASP.NET WebForm where I have the following code for ButtonClear that clears the text that is entered in the TextBoxes:

protected void ButtonClearAll_Click(object sender, EventArgs e)
{
    TextBox1.Text = string.Empty;
    TextBox2.Text = string.Empty;
    TextBox3.Text = string.Empty;
    TextBox4.Text = string.Empty;
}

Code in .aspx file:

<asp:Button ID="ButtonClearAll" runat="server" Text="Clear" OnClick="ButtonClearAll_Click"/>

Problem: By clicking ButtonClearAll once, the four TextBoxes are clearing fine. But on subsequent click, the RequiredFieldValidators of the 4 TextBoxes are getting called and are validating the assigned TextBoxes to print the appropriate error statements next to each TextBox.

What to do?

  • 1
    Learn Javascript. Forget the Validators. Realise that life is tough once you step off Microsoft's beaten path, and that even though it's initially tougher, you're far better off knowing each tech in isolation, free of proprietorial stabilisers that seem to lower the barrier of entry, but actually just bring that barrier crashing down on your head the minute you want something different. – Paul Alan Taylor Dec 12 '17 at 14:36
  • 1
    I know. And to be frank, I am experiencing that the hard way. #JavaScriptIsDope –  Dec 12 '17 at 14:48
  • We all do, sir. I arrived on the .NET scene in 2001. I was MS's target audience, the boy who didn't know web programming but fancied doing it anyway. By 2004, I'd banned every server control at our firm except Repeaters and Literals. – Paul Alan Taylor Dec 12 '17 at 16:32

1 Answers1

2

By default any button, invoking any post-back, is going to invoke the validation first. You can disable validation for a particular button with the CausesValidation property:

<asp:Button ID="ButtonClearAll" CausesValidation="false" ... />
David
  • 208,112
  • 36
  • 198
  • 279