网格视图验证

本文关键字:验证 视图 网格 | 更新日期: 2023-09-27 17:56:46

我正在使用网格视图并使用项模板文本框。有 5 个文本框,我在单击添加行 btn 时动态添加新行。在该行中,我添加了所有空的文本框。现在我想在 btn 上验证该文本框,单击下一步。

现在开始做?

我使用必填字段验证它第一次显示文本框,但是当我添加新的行文本框时,它不会导致验证,我认为还有其他方法可以为动态添加的文本框进行验证。

如何验证所有动态添加的文本框

我正在使用的网格视图..

            <Columns >
                    <asp:TemplateField>
                    <ItemTemplate>
                        <div class="otherOthersTd">
                            <asp:Label ID="lblcnt" ForeColor="#136A96" Text='<%#Eval("Count") %>' runat="server" ></asp:Label>
                            <asp:Label ID="lblId" runat="server" text='<%#Eval("Id") %>' Visible="false"></asp:Label>
                        </div>
                    </ItemTemplate>
                    </asp:TemplateField>
                   <asp:TemplateField HeaderText="First Name">
                            <ItemTemplate>
                                <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>' Width="88px"></asp:TextBox>
                            </ItemTemplate>

                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Middle Name">
                            <ItemTemplate>
                                <asp:TextBox ID="txtMName" runat="server" Text='<%# Eval("MName") %>' 
                                     Width="88px"></asp:TextBox>
                            </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Last Name">
                            <ItemTemplate>
                                <asp:TextBox ID="txtLName" runat="server" Text='<%# Eval("LName") %>' 
                                     Width="88px"></asp:TextBox>
                            </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Degree">
                            <ItemTemplate>
                                <asp:TextBox ID="txtDegree" runat="Server" Text='<%# Eval("Degree") %>' 
                                    Width="50px"></asp:TextBox> 
                            </ItemTemplate>

                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Title">
                            <ItemTemplate>
                                <asp:TextBox ID="txtTitle" runat="Server" Text='<%# Eval("Title") %>' Width="88px"></asp:TextBox>
                            </ItemTemplate>

                    </asp:TemplateField>
                <asp:TemplateField HeaderText="Email">
                     <ItemTemplate> 
                            <asp:TextBox ID="txtEmail" runat="Server" 
                                Text='<%# Eval("Email") %>' Width="88px" CausesValidation="True" ValidationGroup="a"></asp:TextBox>
                        <asp:RegularExpressionValidator ID="RegExpr" runat="server" ErrorMessage="Invalid email id" ControlToValidate="txtEmail" ValidationGroup="a" ValidationExpression="'w+([-+.']'w+)*@'w+([-.]'w+)*'.'w+([-.]'w+)*"></asp:RegularExpressionValidator>
                         <asp:Label ID="revexp" runat="server"  > </asp:Label>
                            </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Institution">
                        <ItemTemplate>
                            <asp:TextBox ID="txtInstitution" runat="server" Text='<%#Eval("Institution") %>'></asp:TextBox>
                        </ItemTemplate>
                </asp:TemplateField>

     <asp:Button ID="btnNext" runat="server" ValidationGroup="a" CausesValidation="true" Text="Next" class="next btn" onclick="btnNext_Click"/>        

                    <asp:TemplateField>  <ItemTemplate>
                     <asp:Label ID="lblAuthor" runat="server" Text="Authorerror" Visible="false"  ForeColor="Red" Font-Bold="True" Font-Size="X-Large">*</asp:Label>
        </ItemTemplate>
                    </asp:TemplateField>

            </Columns>
            <FooterStyle BackColor="#CCCC99" />
            <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="false" ForeColor="White" />
            <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
            <HeaderStyle  Font-Bold="false" ForeColor="#136A96" />
    </asp:GridView>

我的代码...

protected void GridView1_OnRowCommand1(object sender, GridViewCommandEventArgs e)
        {
        if (e.CommandName.Equals(""))
        {
            lstAuthors = (List<OtherAuthors>)Session["lstAuthors"];
            if (lstAuthors.Count == 0)
            {
                OtherAuthors obj0 = new OtherAuthors();
                obj0.Count = "Author 1:";                    
                lstAuthors.Add(obj0);
            }
            int index=GridView1.Rows.Count-1;
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                TextBox Name = GridView1.Rows[i].FindControl("txtName") as TextBox;
                TextBox MName = GridView1.Rows[i].FindControl("txtMName") as TextBox;
                TextBox LName = GridView1.Rows[i].FindControl("txtLName") as TextBox;
                TextBox Degree = GridView1.Rows[i].FindControl("txtDegree") as TextBox;
                TextBox Title = GridView1.Rows[i].FindControl("txtTitle") as TextBox;
                TextBox Email = GridView1.Rows[i].FindControl("txtEmail") as TextBox;
                TextBox Institution = GridView1.Rows[i].FindControl("txtInstitution") as TextBox;
                if(Name!=null)
                {
                lstAuthors[i].Name = Name.Text;
                lstAuthors[i].MName = MName.Text;
                lstAuthors[i].LName = LName.Text;
                lstAuthors[i].Degree = Degree.Text;
                lstAuthors[i].Title = Title.Text;
                lstAuthors[i].Email = Email.Text;
                lstAuthors[i].Institution = Institution.Text;
                }
            }
            OtherAuthors obj1 = new OtherAuthors();
            obj1.Count = "Author "+(lstAuthors.Count+1).ToString()+":";
           obj1.Name="";
           lstAuthors.Add(obj1);
           GridView1.DataSource = lstAuthors;
           GridView1.DataBind();
           for (int i = 0; i < GridView1.Rows.Count; i++)
           {
               if (GridView1.Rows.Count - 1 == i)
                   GridView1.Rows[i].Cells[8].Visible = true;
               else
                   GridView1.Rows[i].Cells[8].Visible = false;
           }
           Session["lstAuthors"] = lstAuthors;
        }
        MultipleModalitySelect1.hideChosebutton = true;
    }

私人布尔值 伊斯瓦利德() { 布尔误差 = 假;

            TextBox Name = GridView1.Rows[0].FindControl("txtName") as TextBox;
            TextBox MName = GridView1.Rows[0].FindControl("txtMName") as TextBox;
            TextBox LName = GridView1.Rows[0].FindControl("txtLName") as TextBox;
            TextBox Degree = GridView1.Rows[0].FindControl("txtDegree") as TextBox;
            TextBox Title = GridView1.Rows[0].FindControl("txtTitle") as TextBox;
            TextBox Email = GridView1.Rows[0].FindControl("txtEmail") as TextBox;
            TextBox Institution = GridView1.Rows[0].FindControl("txtInstitution") as TextBox;
            Label lblAuthor = GridView1.Rows[0].FindControl("lblAuthor") as Label;
            if (Name.Text.Length == 0 || LName.Text.Length == 0 || Degree.Text.Length == 0 ||Title.Text.Length == 0 || Email.Text.Length == 0 || Institution.Text.Length == 0)
            {
                lblAuthor.Visible = true;
                error = true;
            }
            else
            {
                lblAuthor.Visible = false;
            }

}

网格视图验证

您已在"下一步"按钮上使用验证组的ValidationGroup="a",但尚未在必填字段验证器上使用该。但是您已经在电子邮件验证器中使用了它。

您必须与验证控件保持一致,以及是否对所有控件和按钮控件进行验证才能使其正常工作。

<asp:Button ID="btnNext" runat="server" ValidationGroup="a" CausesValidation="true" Text="Next" class="next btn" onclick="btnNext_Click"/>

我认为您应该开始一个新页面并使用更小的案例场景进行测试,并首先使其正常工作。 然后添加更多框。

如果您仍然需要有关该较小页面的帮助,请发布它以寻求帮助 - 我们将更容易理解它。