How to place a required field validator inside a GridView TextBox

别说谁变了你拦得住时间么 提交于 2019-12-02 10:14:07

In the Edit template, add a RequiredFieldValidator like this:

<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>

Here is the reference for the RequiredFieldValidator on MSDN.

UPDATE:

If you wanted a regular expression validator, its pretty much the same, but with the RegularExpressionValidator control:

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

Here is a complete list of the functionality for the RegularExpressionValidator on MSDN.

Within gridview i assign textbox,requiredfieldvalidator and button,This validator validate all textboxes in gridview when button click without filling textbox. How can i solve this..

    <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>

This will validate all textboxs in gridview,When i click button in particular row without filling the textbox in itemtemplate.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!