How to place a required field validator inside a GridView TextBox

I have a GridView with some TemplateField items containing TextBox controls. I would like to add a required field validator on it. This is my code:

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

How do I place a required field validator on txtvid?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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.

Solution 2

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.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply