开发者

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

开发者 https://www.devze.com 2023-02-05 22:25 出处:网络
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 clic开发者_JAVA百科k anyway where on the page,

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 clic开发者_JAVA百科k 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 !


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


Set CausesValidation to false for the other posting controls.

<asp:Button CausesValidation="false" Text="Cancel" Name="CancelButton" />
0

精彩评论

暂无评论...
验证码 换一张
取 消