【问题标题】:How to place a required field validator inside a GridView TextBox如何在 GridView 文本框内放置必填字段验证器
【发布时间】:2013-10-02 07:33:14
【问题描述】:

我有一个 GridView 和一些包含 TextBox 控件的 TemplateField 项目。我想在上面添加一个必填字段验证器。这是我的代码:

<asp:TemplateField HeaderText="vid">
    <EditItemTemplate>
         <asp:TextBox ID="txtvid" runat="server" Width="150px"
                            Text='<%# Bind("vid") %>'>
         </asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
         <asp:Label 
                   ID="lblvid" runat="server" 
                   Text='<%# Bind("vid") %>'>
         </asp:Label>
    </ItemTemplate>
 </asp:TemplateField>

如何在txtvid 上放置必填字段验证器?

【问题讨论】:

    标签: asp.net gridview requiredfieldvalidator


    【解决方案1】:

    在编辑模板中,像这样添加RequiredFieldValidator

    <EditItemTemplate>
        <asp:TextBox ID="txtvid" 
                     runat="server" Width="150px"
                     Text='<%# Bind("vid") %>'>
        </asp:TextBox>
        <asp:RequiredFieldValidator 
                     ControlToValidate="txtvid" 
                     runat="server" 
                     ErrorMessage="Please enter a 'vid' number" 
                     Text="*"/>
    </EditItemTemplate>
    

    这是 MSDN 上RequiredFieldValidator 的参考。

    更新:

    如果您想要一个正则表达式验证器,它几乎相同,但使用 RegularExpressionValidator 控件:

     <asp:RegularExpressionValidator 
         ControlToValidate="txtvid"
         ValidationExpression="\d{10}"
         runat="server" 
         ErrorMessage="Please enter a 'vid' of 10 digits" 
         Text="*"/>
    

    这是 MSDN 上 RegularExpressionValidator 功能的完整列表。

    【讨论】:

    • 基本上我使用正则表达式验证器,并且已经自己尝试了该代码,但它不起作用
    • 必填字段验证器工作正常,但正则表达式在这里不起作用
    • @amitesh:但你的问题是:“如何在 gridview 文本框中输入必填字段验证”
    • sry 伙计们,但是是的,我使用正则表达式验证器来管理你的代码工作,它可以工作,感谢 mate 和感谢 tim
    【解决方案2】:

    在 gridview 中我分配了文本框、requiredfieldvalidator 和按钮,当单击按钮而不填充文本框时,此验证器会验证 gridview 中的所有文本框。我该如何解决这个问题。。

        <asp:TemplateField HeaderText="vid">               
            <ItemTemplate>
        <asp:TextBox ID="txtvid" runat="server" Width="150px" ValidationGroup ="subgrp">
                 </asp:TextBox>
    <asp:RequiredFieldValidator ID="rfvQuantity" ControlToValidate="txtvid" runat="server"
                        ErrorMessage="Required" ForeColor="Red"
                        ValidationGroup = "subgrp"></asp:RequiredFieldValidator>
                 <asp:Label 
                           ID="lblvid" runat="server" 
                           Text='<%# Bind("vid") %>'>
                 </asp:Label>
    <asp:Button ID="btnSelect" runat="server" Text="Select" ValidationGroup ="subgrp"/>
            </ItemTemplate>
         </asp:TemplateField>
    

    这将验证gridview中的所有文本框,当我单击特定行中的按钮而不填充itemtemplate中的文本框时。

    【讨论】:

    • 设置你的按钮属性 CauseValidation = "false" 。因为它默认为 true。
    猜你喜欢
    • 2015-02-20
    • 2017-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多