如何为 gridview 中的文本框提供必填字段验证器?
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Height="146px"
Width="308px">
<Columns>
<asp:TemplateField HeaderText="Original Price" ControlStyle-Width="100px">
<ItemTemplate>
<asp:TextBox ID="txtOriginalPrice" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfv" runat="server" ControlToValidate="txtOriginalPrice"
ValidationGroup="GridView1" Display="Static" ErrorMessage="" Text="*"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
我正在使用上面的代码,但它不起作用,尽管我已经放置了 requirefield 验证器,但它没有向我显示“*”
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,您的 requiredfieldvalidator 肯定在那里,并且它知道要验证哪个控件。缺少的是“何时验证该控件”。要回答这个问题,您需要将文本框添加到与 requiredfieldvalidator 相同的 ValidationGroup 以及 控件(例如,这可以是一个按钮)导致进行验证。因此您的代码将类似于
所以不要忘记这些问题
所有这些控件必须具有相同的 ValidationGroup。
Yes, your requiredfieldvalidator is certainly there, and it knows which control to validate.What is missing is "when to validate that control".And to answer this question you need to add your textbox the same ValidationGroup with your requiredfieldvalidator and also the control(this can be a button for example) causes to do a validation.So your code will be like
So don't forget these question
All of these controls must have the same ValidationGroup.
如果您需要执行验证,请检查类似
因此,现在当您单击添加按钮时,它将验证属于您提到的验证组的那些控件,否则默认值为“”,因此您不会发现任何触发的验证。
In case you need the validation to be performed then check for something like this
<asp:Button ID="btnAdd" runat='server' ValidationGroup='GridView1' CausesValidation='true'.....
So now when you click the add button it will validate for those controls falling under the validation group you mentioned else the default value is "" hence you won't find any validation triggered.
将 ValidationGroup 添加到文本框 (
txtOriginalPrice
)、Button 等控件。Add ValidationGroup to TextBox (
txtOriginalPrice
), Button and other controls.