locked
how to validate a text box in a gridview control ie in item template field RRS feed

  • Question

  • User31862606 posted

    I'm having a editable grid view as shown below with a text box in item template and i'm getting a dataset from an oracle ref cursor and assigning it to the gridview datasource now i want to validate the textbox for required field validation and simultaniously check whether the user entered a number instead of alphabets using regex? 

    anyone who has ideas are welcomed to share their thoughts below is the gridview control design(source)

    thanks in advance

    jeeva

    <asp:GridView ID="gvRevenue" runat="server" AutoGenerateColumns="False"
    GridLines="None" onrowdatabound="gvRevenue_RowDataBound" cellspacing="10">

    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%# Bind("INDENT1") %>'></asp:Label>
    </ItemTemplate>
    <ItemStyle VerticalAlign="Top"/>
    </asp:TemplateField>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:Label ID="Label2" runat="server" Text='<%# Bind("INDENT2") %>'></asp:Label>
    </ItemTemplate>
    <ItemStyle VerticalAlign="Top"/>
    </asp:TemplateField>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:Label ID="Label3" runat="server" Text='<%# Bind("DESCRIPTION") %>'></asp:Label>
    </ItemTemplate>
    <ItemStyle VerticalAlign="Top" Height="20"/>
    </asp:TemplateField>
    <asp:TemplateField Visible="false">
    <ItemTemplate>
    <asp:Label ID="Label4" runat="server" Text='<%# Bind("BOLD") %>'></asp:Label>
    </ItemTemplate>
    <ItemStyle VerticalAlign="Top" />
    </asp:TemplateField>
    <asp:TemplateField Visible="false">
    <ItemTemplate>
    <asp:Label ID="Label5" runat="server" Text='<%# Bind("AMOUNTDISPLAY") %>'></asp:Label>
    </ItemTemplate>
    <ItemStyle VerticalAlign="Top"/>
    </asp:TemplateField>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("AMOUNT") %>'></asp:TextBox>

    </ItemTemplate>
    <ItemStyle VerticalAlign="Top"/>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>

    Thursday, June 27, 2013 11:08 AM

Answers

  • User143067745 posted

    You can use RequiredFieldValidator and RegularExpressionValidator on GridView's  TextBox as:

    <asp:GridView ID="gvRevenue" runat="server" AutoGenerateColumns="False" 
    GridLines="None" onrowdatabound="gvRevenue_RowDataBound" cellspacing="10">
    
    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%# Bind("INDENT1") %>'></asp:Label>
    </ItemTemplate>
    <ItemStyle VerticalAlign="Top"/>
    </asp:TemplateField>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:Label ID="Label2" runat="server" Text='<%# Bind("INDENT2") %>'></asp:Label>
    </ItemTemplate>
    <ItemStyle VerticalAlign="Top"/>
    </asp:TemplateField>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:Label ID="Label3" runat="server" Text='<%# Bind("DESCRIPTION") %>'></asp:Label>
    </ItemTemplate>
    <ItemStyle VerticalAlign="Top" Height="20"/>
    </asp:TemplateField>
    <asp:TemplateField Visible="false">
    <ItemTemplate>
    <asp:Label ID="Label4" runat="server" Text='<%# Bind("BOLD") %>'></asp:Label>
    </ItemTemplate>
    <ItemStyle VerticalAlign="Top" />
    </asp:TemplateField>
    <asp:TemplateField Visible="false">
    <ItemTemplate>
    <asp:Label ID="Label5" runat="server" Text='<%# Bind("AMOUNTDISPLAY") %>'></asp:Label>
    </ItemTemplate>
    <ItemStyle VerticalAlign="Top"/>
    </asp:TemplateField>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("AMOUNT") %>'></asp:TextBox>
    <asp:RequiredFieldValidator ControlToValidate="TextBox1" Text="*" runat="server" SetFocusOnError="true"/>
     <asp:RegularExpressionValidator ID="regexpTGridText" runat="server"     
                                        ErrorMessage="*Invalid" 
                                        ControlToValidate="TextBox1"     
                                        ValidationExpression="[0-9]*" />
    </ItemTemplate>
    <ItemStyle VerticalAlign="Top"/>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 27, 2013 10:55 PM