ControlToValidate 在所有单击时都会触发:我只希望它在单击“提交”时触发按钮
我有一个经过验证的文本框,以确保文件上传页面有文本内容。
我的问题是,当页面首次加载并且您单击页面上的任意位置时,ControlToValidate 会立即触发,这是一个问题,因为我只希望页面在您单击上传按钮时验证文本框。
您最终会被困在“上传文件”页面上,直到您在文本框中键入一些随机文本,即使您单击链接以访问网站中的其他位置(即返回主页的徽标)。
这是我的相关代码片段:
<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>
所以我有几个想法,但不确定如何实现它们:
1)找到一些属性,将 ControlToValidate 仅链接到单击上传按钮。
或
2)设置ControlToValidate,以便仅在回发时调用它
我只是不确定从哪里开始,并且无法找到专门执行此操作的任何属性!
非常感谢您的宝贵时间!
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 !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将按钮和要验证的控件的 ValidationGroup 属性设置为相同的值。
Set the ValidationGroup property of the button and the controls to validate to the same value.
将其他发布控件的
CausesValidation
设置为 false。Set
CausesValidation
to false for the other posting controls.