2011-05-16 6 views
4

Gridview를 사용하고 Item Template 텍스트 상자를 사용하고 있습니다. 5 개의 텍스트 상자가 있고 행 추가 btn을 클릭하면 새 행이 동적으로 추가됩니다. 그 행에 내가 비어있는 모든 텍스트 상자를 추가했습니다. 이제 btn에서 해당 텍스트 상자를 확인하려면 다음을 클릭하십시오.Gridview Validation

이제할까요?

필자는 필요한 필드 유효성 검사를 처음으로 사용하여 텍스트 상자를 표시했지만 새 행 텍스트 상자에 유효성 검사를 발생시키지 않으면 동적으로 추가 된 텍스트 상자에 대한 유효성을 검사하는 다른 방법이 있다고 생각합니다.

난 내 모든 동적으로 추가 텍스트 상자 내가 사용하고

그리드보기 ..

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

    } 

개인용 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

+0

@Ansari; 유효성 검사 문제는 런타임에 컨트롤을 추가 할 때 발생합니까? 그렇지 않으면 완벽하게 작동합니까? 귀하의 문제를 파악할 수 있도록 전체 코드를 게시하십시오. –

답변

3

다음 단추에서 유효성 검사 그룹 ValidationGroup="a"을 사용했지만 필수 필드 검사기에서 유효성 검사 그룹을 사용하지 않았습니다. 하지만 이메일 유효성 검사기에서 사용했습니다.

유효성 검사 컨트롤 및 유효성 검사를 위해 모든 컨트롤과 단추 컨트롤에 유효성 검사를 적용할지 여부는 일관되어야합니다.

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

난 당신이 훨씬 작은 경우의 시나리오와 새로운 페이지 및 테스트를 시작하고 첫 번째 작업 것을 얻을한다고 생각합니다. 그런 다음 상자를 추가하십시오.

작은 페이지에 대한 도움이 여전히 필요하면 도움을 위해 게시하십시오. 이해하기 쉽습니다.

+0

당신은 대답 대신에이 글을 써야합니다. –