Editing a textbox in a gridview row. Required Field problem

Jan 14 at 6:41 PM
Edited Jan 14 at 6:42 PM
I have a web page with a gridview control. I allow users to enter serial numbers directly in a textbox on each row of the gridview. I do not use Edit / Cancel / Update. I want to allow them to enter serial numbers for any row they wish directly in the gridview. Here is my ASP.Net code:
<tr>
            <td>
<asp:GridView ID="gvItems" 
                                  runat="server"
                                  AutoGenerateColumns="False" 
                                  DataKeyNames="RmaDetailId" 
                                  HeaderStyle-HorizontalAlign="left" 
                                  CellSpacing="5"
                                  HeaderStyle-ForeColor="Maroon"
                                  RowStyle-ForeColor="DarkBlue"
                                  AlternatingRowStyle-ForeColor="DarkBlue" 
                                  EmptyDataText="No RMA Item rows exist" 
                                  OnRowDataBound="gvItems_RowDataBound">
                                    <Columns>
                                        <asp:TemplateField HeaderText="Sku">
                                            <ItemTemplate>
                                                <asp:Label ID="lblSku" runat="server" Text='<%# Eval("RmaSku") %>'>
                                                </asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>  
                                        <asp:TemplateField HeaderText="Description">
                                            <ItemTemplate>
                                                <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'>
                                                </asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField> 
                                        <asp:TemplateField HeaderText="Serial Number">
                                            <ItemTemplate>
                                                <asp:TextBox ID="txtSerialNum" runat="server" Text='<%# Eval("SerialNum") %>'>
                                                </asp:TextBox>
                                                <asp:RequiredFieldValidator ID="rfv1" 
                                                                            runat="server" 
                                                                            ControlToValidate="txtSerialNum" 
                                                                            ErrorMessage="*" 
                                                                            ForeColor="Red">
                                                </asp:RequiredFieldValidator>
                                            </ItemTemplate>                                            
                                        </asp:TemplateField>                                                                   
                                    </Columns>
                    </asp:GridView>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="lblErrorsExist1" runat="server" ForeColor="Red" Font-Bold="true" Font-Size="18px" Visible="false">Please Complete All Required Fields </asp:Label>
            </td>
        </tr>       
        <tr>
            <td>
                <asp:Button ID="btnSubmit" runat="server" Text="Submit" Font-Bold="True" ForeColor="Maroon" OnClick="btnSubmit_Click" />
            </td>
        </tr>
Here is my C# code for the button click event:
protected void btnSubmit_Click(object sender, EventArgs e)
        {

                foreach (GridViewRow dr in gvItems.Rows)
                {
                    Label lRmaDetailId = dr.FindControl("lblRmaDetailId") as Label;
                    Label lSku = dr.FindControl("lblSku") as Label;
                    Label lProbErr = dr.FindControl("lblProblemError") as Label;
                    Label lBattErr = dr.FindControl("lblBattError") as Label;
                    Label lSerReq = dr.FindControl("lblRmaSerialNumRequired") as Label;
                    TextBox sn = dr.FindControl("txtSerialNum") as TextBox;
                    DropDownList ddl = dr.FindControl("ddlProblem") as DropDownList;
                    DropDownList ddl2 = dr.FindControl("ddlBatteryConnector") as DropDownList;

                    Session["ErrorsExist"] = "";
                    lblErrorsExist1.Visible = false;

                    if (lSerReq.Text == "Y")
                    {
                        if (sn.Text.Length == 0)
                        {
                            lblErrorsExist1.Visible = true;
                            Session["ErrorsExist"] = "Y";
                        }
                        else
                        {
                            sn.BackColor = Color.White;
                        }
                    }
                }                    
        }
But when I enter a serial number value, the page displays the error message saying "Please Complete All Required Fields". While in debug mode, sn.Text.Length is = 0. But I enter a value like "XXXXXXX". What am I doing wrong?
Jan 15 at 7:04 PM
I figured it out.

Moderator: You can remove this post.
Marked as answer by bh0526 on 1/15/2016 at 12:04 PM