2011-05-16 17 views
4

我正在使用Gridview并使用Item Template文本框。有5个文本框,我点击添加行btn动态地添加一个新行。在那一行中,我添加了所有空的文本框。现在我想在btn上验证该文本框,然后单击下一步。Gridview验证

现在要做?

我使用了必需的字段验证其第一次显示文本框的工作,但是当我添加一个新的行文本框它不会导致验证,我认为有一些其他方式来验证动态添加文本框。

我怎么能确认我的所有动态添加文本框里面我是用

网格视图..

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

    } 

private bool IsValied() { bool error = false;

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

}

+0

你有什么代码? – soandos 2011-05-16 04:16:39

+0

@Ansari;验证问题是在运行时添加控件的时候?否则,它的工作完美?请发布您的完整代码,以便我们能够确定您的问题。 – 2011-05-16 04:27:32

回答

3

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

您必须与验证控件一致,以及是否对所有控件和按钮控件进行验证才能使其生效。

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

我想你应该开始一个新的页面,并测试一个小得多的情况,并得到该工作第一。然后添加更多框。

如果您仍然需要该小页面的帮助,请发布求助信息 - 这对我们来说会更容易理解。

+0

你应该在评论中写下这个,而不是回答。 – 2011-05-16 04:40:07