简体   繁体   English

如何将所需的字段验证器赋予gridview中的文本框?

[英]how to give required field validator to textbox in 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>

I am using above code but its not working though I have put requirefield validator it does not show me the '*' 我正在使用上面的代码,但是虽然我已经将requirefield验证器放到了它上面,但它却没有显示“ *”

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 是的,您的requiredfieldvalidator当然在那里,它知道要验证的控件。缺少的是“何时验证该控件”。要回答此问题,您需要为文本框添加与requiredfieldvalidator和控件相同的ValidationGroup。 例如,这可以是一个按钮),导致进行验证 。因此,您的代码将像

<%--<asp:Button ID="Button1" runat="server" ValidationGroup="GridView1" Text="Benjamin"...Somewhere in your code--%>    
    <asp:TextBox ID="txtOriginalPrice" runat="server" ValidationGroup="GridView1"></asp:TextBox> 

So don't forget these question 所以不要忘记这些问题

  1. What to validate?(a textbox) 验证什么?(文本框)
  2. When to validate?(after a button click) 何时验证?(单击按钮后)
  3. After what action try to validate?(a button click) 尝试执行什么操作后进行验证?(单击按钮)
  4. With what to validate?(a requiredfieldvalidator) 用什么来验证?(必填字段验证器)

All of these controls must have the same ValidationGroup. 所有这些控件必须具有相同的ValidationGroup。

ValidationGroup添加到TextBox( txtOriginalPrice ),Button和其他控件。

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. 因此,现在单击添加按钮时,它将对您提到的验证组下的那些控件进行验证,否则默认值为“”,因此您将找不到触发的任何验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM