We help IT Professionals succeed at work.

How to validate asp textbox in side gridview

moe57
moe57 asked
on
I am trying to validate asp textbox inside grid-view but my challenge is that i have 3 item templates in my aspx page,
ID
Question
Answer
The questions are generated dynamically after the grid view.bind() is called but i want to put some validation on the Answer field where users have to enter specific answer only based on the ID.  For example, If ID = 1 then they can only enter age number like 1 to 99. If ID = 2 then they can only enter some kind height values like 5.3, 5.8 etc. Here is how my current grid view look like
<asp:GridView ID="GV_Test" runat="server"  
                    AutoGenerateColumns="False"
                    ShowFooter="True" DataKeyNames="ID">
                    <Columns>

                        <asp:TemplateField  HeaderText="Question ID" Visible="true">
                            <ItemTemplate>
                                <asp:Label ID="lblQST_SK" runat="server" Text='<%# Eval("ID")%>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>


                        <asp:TemplateField ItemStyle-Width="150px" HeaderText="Questions">
                            <ItemTemplate>
                                <asp:Label ID="lblQuestions" runat="server" Text='<%# Eval("Question")%>'></asp:Label>
                            </ItemTemplate>                                
                        </asp:TemplateField>

                        <asp:TemplateField HeaderText="Answer">                              
                                <ItemTemplate>                               
                                  <asp:TextBox ID="txtAn" runat="server" CssClass="form-control" Text='<%# Eval("Answer")%>' ></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>

                    </Columns>

                </asp:GridView>

Open in new window

I believe i have to use some kind of javascript that uses some kind event like onkeyup or onclick() something like this but not sure how to use it.   Please help.   thanks
Comment
Watch Question

Technical Analyst
Commented:
Dear moe57,

You can perform your task by RowDataBound Event of Gridview. Here are the steps to follow for your reference:

Step 1: Add the Validators in your design page (.aspx) with Answer TextBox
<asp:TemplateField HeaderText="Answer">                              
  <ItemTemplate>                               
    <asp:TextBox ID="txtAn" runat="server" CssClass="form-control" Text='<%# Eval("Answer")%>' ></asp:TextBox>
    <asp:RequiredFieldValidator ID="rfvAnswer" ControlToValidate="txtAn" runat="server" ErrorMessage="Kindly Enter Answer"></asp:RequiredFieldValidator>
    <asp:RegularExpressionValidator ID="revAnswer" ControlToValidate="txtAn" runat="server" ErrorMessage="Kindly Enter Proper Value"></asp:RegularExpressionValidator>
   </ItemTemplate>
</asp:TemplateField>

Open in new window


I have added the RequiredFieldValidator too. You can remove if not required. ☺

Step 2: Add the RowDataBound Event of the Gridview
protected void gvShipmentStatus_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    Label lblQST_SK = (Label)e.Row.FindControl("lblQST_SK");
                    RequiredFieldValidator rfvAnswer = (RequiredFieldValidator)e.Row.FindControl("rfvAnswer");
                    RegularExpressionValidator revAnswer = (RegularExpressionValidator)e.Row.FindControl("revAnswer");

                    if (lblQST_SK != null)
                    {
                        switch (lblQST_SK.Text)
                        {
                            case "1":
                                revAnswer.ValidationExpression = "[0-99]";
                                break;
                            case "2":
                                revAnswer.ValidationExpression = "[-+]?([0-9]*\\.[0-9]+|[0-9]+)";
                                break;

                            // and so on for your ID Values you can give the regular expression as you like. 
                        }
                    }
}
}

Open in new window


Hope this helps solve your problem.

Regards,
Kaushal Arora

Author

Commented:
Kaushal Arora, you are great man thank for your help.  How can i put a time validation? I need to put military time validation?  thanks