ControlToValidate fires on all clicks: I only want it to fire when I click the “Submit” button

StackOverflow https://stackoverflow.com/questions/4727978

  •  12-10-2019
  •  | 
  •  

Question

I have a textbox that is validated to make sure there is text content, for a file upload page.

My problem is that when the page has first loaded and you click anyway where on the page, the ControlToValidate fires immediately, this is an issue because I only want the page to validate the text box when you click the upload button.

You end up being trapped on the Upload File page until you type some random text into the textbox, even if you click a link to get somewhere else in the site (i.e the logo to get back to the mainpage).

Here is my relevant code snippet:

 <asp:Label runat="server">Name</asp:Label>:
<br />
<asp:TextBox runat="server" ID="txtName"></asp:TextBox><asp:RequiredFieldValidator
    runat="server" ID="txtNameRequired" Display="Dynamic" ControlToValidate="txtName"
    Text="Name is required." CssClass="errorText" /><br />

<asp:Button runat="server" ID="btnUpload" Text="Upload" OnClick="btnUpload_Click" />
</asp:Panel> 

So I have a couple ideas but was not sure how to implement them :

1) find some property that will link the ControlToValidate solely to the clicking of the upload button.

or

2) Set up ControlToValidate so that it is only called on PostBacks

I am just not sure where to start and haven't been able to find any properties that specifically do this!

Thank you very much for your time !

Was it helpful?

Solution

Set the ValidationGroup property of the button and the controls to validate to the same value.

OTHER TIPS

Set CausesValidation to false for the other posting controls.

<asp:Button CausesValidation="false" Text="Cancel" Name="CancelButton" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top