2012-09-06 2 views
0

동적 인 HTML 테이블에 유효성 검사를 넣는 방법 asp.net C#으로 동적 인 html 테이블에 유효성 검사를 넣는 방법? ? RequireField 및이 코드가있는 다른 유효성 검사기를 원합니다. . 보호 된 무효 Page_Load (개체 보낸 사람, EventArgs 전자) { if (! IsPostBack) { HtmlTable dtbl = new HtmlTable(); dtbl.Border = 5;asp.net C#

 dtbl.BorderColor = "black"; 
     dtbl.CellPadding = 6; 
     dtbl.CellSpacing = 1; 
     HtmlTableRow row1; 
     for (int i = 0; i <= 2; i++) 
     { 
      row1 = new HtmlTableRow(); 
      row1.BgColor = "Orange"; 

      if (i == 0) 
      { 
       HtmlTableCell cell; 
       for (int j = 1; j <= 4; j++) 
       { 
        cell = new HtmlTableCell(); 
        cell.Align = "center"; 
        switch (j) 
        { 
         case 1: 
          Label l1 = new Label(); 
          l1.Text = "Name"; 
          l1.ForeColor = Color.White; 
          cell.Controls.Add(l1); 
          break; 

         case 2: 
          Label l2 = new Label(); 
          l2.Text = "Age"; 
          l2.ForeColor = Color.White; 
          cell.Controls.Add(l2); 
          break; 

         case 3: 
          Label l3 = new Label(); 
          l3.Text = "Gender"; 
          l3.ForeColor = Color.White; 
          cell.Controls.Add(l3); 
          break; 

         case 4: 
          Label l4 = new Label(); 
          l4.Text = "EmailId"; 
          l4.ForeColor = Color.White; 
          cell.Controls.Add(l4); 
          break; 
        } 
        row1.Controls.Add(cell); 
       } 

      } 
      if (i == 1) 
      { 
       HtmlTableCell cell; 
       for (int j = 1; j <= 4; j++) 
       { 
        cell = new HtmlTableCell(); 
        cell.Align = "center"; 
        switch (j) 
        { 
         case 1: 
          TextBox t1 = new TextBox(); 
          t1.ID = "txtName"; 
          t1.ForeColor = Color.Green; 
          cell.Controls.Add(t1); 
          break; 

         case 2: 
          TextBox t2 = new TextBox(); 
          t2.ID = "txtAge"; 
          t2.ForeColor = Color.Green; 
          cell.Controls.Add(t2); 
          break; 

         case 3: 
          RadioButtonList rbl1 = new RadioButtonList(); 
          rbl1.ID = "rbl"; 
          rbl1.RepeatDirection = RepeatDirection.Horizontal; 
          rbl1.Items.Add(new ListItem("Male","Male")); 
          rbl1.Items.Add(new ListItem("Female", "Female")); 
          cell.Controls.Add(rbl1); 


          break; 

         case 4: 
          TextBox t4 = new TextBox(); 
          t4.ID = "txtEmail"; 
          t4.ForeColor = Color.Green; 
          cell.Controls.Add(t4); 
          break; 
        } 
        row1.Controls.Add(cell); 
       } 
      } 
      if (i == 2) 
      { 
       HtmlTableCell cell = new HtmlTableCell(); 
       cell.ColSpan = 4; 
       Button b1 = new Button(); 
       cell.Align = "center"; 
       b1.Style.Add(HtmlTextWriterStyle.BackgroundColor,"Green"); 
       b1.Style.Add(HtmlTextWriterStyle.Color, "White"); 
       b1.Style.Add(HtmlTextWriterStyle.BorderStyle, "solid"); 
       b1.Style.Add(HtmlTextWriterStyle.BorderColor, "White"); 
       b1.Style.Add(HtmlTextWriterStyle.Height, "25px"); 
       b1.Style.Add(HtmlTextWriterStyle.Width, "100px"); 
       b1.ID = "btnSubmit"; 
       b1.Text = "Submit"; 
       b1.Click+=new System.EventHandler() 
       cell.Controls.Add(b1); 
       row1.Controls.Add(cell); 
      } 
      dtbl.Controls.Add(row1); 

     } 

     form1.Controls.Add(dtbl); 
     Page.Controls.Add(form1); 

    } 
} 

}

답변

2

RequiredFieldValidator 컨트롤을 만듭니다 유효성을 검사 할 컨트롤의 ID로 ControlToValidate 속성을 설정하고 테이블 셀에 새 검사기 컨트롤을 추가합니다.

RequiredFieldValidator req = new RequiredFieldValidator(); 
req.ControlToValidate = txtEmail.ID; 
// set additional properties here 
cell.Controls.Add(req); 
+0

감사합니다. . . . . . – kkk

+0

req.Display = "dynamic"; cell.Controls.Add (req); 오류를 보여줍니다. 동적 표시 방법? – kkk

+0

'req.Display = ValidatorDisplay.Dynamic' – podiluska