2010-02-22 2 views
0

내 코드에서 테이블을 동적으로 만들려고합니다. 내 문제는 얼마나 많은 컨트롤을 각 TableRow에 추가했는지 계산 한 후 그 TableRow를 테이블에 추가 한 다음 새 행을 시작하려는 것입니다. 여기에 내가 멀지 않은 코드가 있지만 한 행을 추가하고 컨트롤을 별도의 새 행으로 이동하지 않습니다.서버 쪽 코드에서 여러 행 테이블을 만들 때 문제가 발생했습니다.

public void FillTable(string DB, int? appID, string function, int? rID, string UserId, int ControlsperRow) 
{ 
    OneEvaDataContext datacontext = new OneEvaDataContext(); 

    var results = datacontext.sp_Build_Menu_Field_Names(DB, appID, function, rID); 
    int controlCount = 0; 

    TableRow tr = new TableRow(); 
    foreach (sp_Build_Menu_Field_NamesResult result in results) 
    { 

      TableCell tcLabel = new TableCell(); 
      TableCell TcControl = new TableCell(); 
      Control control = new Control(); 

      control = DynamicControlReturn.ReturnControl(result.Param_Name, result.Build_Options_Type_NAME, UserId); 
      control.ID = "control" + controlCount.ToString(); 

      _controlClientIDList.Add(control.ClientID); 

      tcLabel.Text = result.Param_Label; 
      TcControl.Controls.Add(control); 
      tr.Cells.Add(tcLabel); 
      tr.Cells.Add(TcControl); 

      controlCount = controlCount + Convert.ToInt32(result.TD_Len); 

      if (controlCount == ControlsperRow) 
      { 
       base.Controls.Add(tr); 
       controlCount = 0; 
      } 
    } 
} 

내가 내가 내가 같은 TableRow를 사용하고 있기 때문에 해당 행이 일 것이다 그러나 나는 추측 구축을 완료하고 때 .Add을하고 전용 "TR"그것은 단지 하나의 행에 넣는 것을 생각했을 것이다.

편집 : 끝에서 foreach 문에

public class Dynamic_Search_Table : System.Web.UI.WebControls.Table 
    { 
+0

난 당신이 당신의 자신의 질문에 대답 생각? – womp

답변

0

이동

TableRow tr = new TableRow(); 

및 추가

this.Add(tr); 

다음이에서 컨텍스트 기반은 내 다음과 같습니다 클래스 foreach의.


편집 : 변경

if (controlCount == ControlsperRow) 
{ 
    base.Controls.Add(tr); 
    controlCount = 0; 
} 

if (controlCount == ControlsperRow) 
{ 
    base.Controls.Add(tr); 
    controlCount = 0; 
    tr = new TableRow(); 
} 
+0

이것은 foreach 루프의 각 항목에 대해 새로운 행을 만들지 만, 항상 그 루프에 새로운 행을 넣고 싶지는 않습니다. –

+0

죄송합니다. 질문을 잘못 읽었습니다. 잠시만 기다려주세요 :) – TJMonk15

관련 문제