2013-03-06 5 views
0

같은 열의 텍스트 상자에서 값을 SUM (추가)하고 "계산"버튼을 클릭하여 텍스트 상자를 마지막 행에 표시합니다.
모든 텍스트 상자는 "표 만들기"버튼을 클릭하면 동적으로 생성됩니다.

나는이 텍스트 상자의 ID를 얻는 방법을 모르겠다.동적 테이블 내에서 동적 텍스트 상자를 처리하려면 어떻게해야합니까?

public partial class stable : System.Web.UI.Page 
{ 
private int tblRow; 
private int tblCol = 9; 
private int i, j; 
private bool CTflag; 

Table table = new Table(); 
TableRow row,rrow; 
TableCell cell,rcell; 
FileUpload fileUp; 
Button UpLdButton; 
TextBox tb,tbr; 
TextBox txtRes; // to store result of calculation 

string cmdArg; // for passing filuploaders id with Command button 
string txtArgs; 

private string filename = "fileUpLoader"; 

private string tbRowId = "row"; 
private string tbColId = "col"; 

protected int Rows 
{ 
    get 
    { 
     return ViewState["Rows"] != null ? (int)ViewState["Rows"] : 0; 
    } 
    set 
    { 
     ViewState["Rows"] = tblRow; 
    } 
} 

// Columns property to hold the Columns in the ViewState 
protected int Columns 
{ 
    get 
    { 
     return ViewState["Columns"] != null ? (int)ViewState["Columns"] : 0; 
    } 
    set 
    { 
     ViewState["Columns"] = tblCol; 
    } 
} 

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (Page.IsPostBack) 
    { 
     this.Rows = tblRow; 
     this.Columns = tblCol; 
     if (CTflag == false) 
     { 
      CreateDynamicTable(); 
     } 
     else 
     { 
      CTflag = true; 
     } 
    } 
    //LoadViewState(object this); 
    //CreateDynamicTable(); 
} 
protected void Button1_Click(object sender, EventArgs e) 
{ 
    //Button1.Visible = false; 
    CreateDynamicTable(); 
} 

protected void Button1_Click(object sender, EventArgs e) 
{ 
    CreateDynamicTable(); 
} 

protected void CreateDynamicTable() 
{ 
    //Button1.Visible = false; 
    tblRow = Convert.ToInt32(DropDownList1.SelectedValue); 
    //Creat the Table and Add it to the Page  
    if (CTflag == false) 
    { 
     //Table table = new Table(); 
     table.Caption = "Challan Entry"; 
     table.ID = "Challan Entry"; 
     table.BackColor = System.Drawing.Color.BurlyWood; 
     Page.Form.Controls.Add(table); 

     // Now iterate through the table and add your controls 
     for (i = 0; i < 1; i++) 
     { 
      row = new TableRow(); 
      row.BorderStyle = BorderStyle.Ridge; 

      for (j = 0; j <= tblCol; j++) 
      { 
       cell = new TableCell(); 
       cell.BorderWidth = 5; 
       cell.BorderStyle = BorderStyle.Ridge; 
       cell.BorderColor = System.Drawing.Color.Azure; 
       for (j = 0; j <= tblCol; j++) 
       { 
        string[] Header = { "CC NO.", "DATE", "TotalAmt", "NoOfRecpt", "Energy", "New", "Theft", "Misc", "SelectFile", "Upload", "Status" }; 
        Label lbl = new Label(); 
        lbl.ID = "lblHeader" + j; 
        if (j == 8) 
        { 
         lbl.Width = 220; 
        } 
        else if (j == 9) 
        { 
         lbl.Width = 50; 
        } 
        else 
        { 
         lbl.Width = 100; 
        } 
        lbl.Text = Header[j]; 

        // Add the control to the TableCell 
        cell.Controls.Add(lbl); 
       } 
       row.Cells.Add(cell); 
      } 

      // Add the TableRow to the Table 
      table.Rows.Add(row); 
     } 
     for (i = 0; i < tblRow; i++) 
     { 
      row = new TableRow(); 
      row.ID = tbRowId + i; 
      row.BorderStyle = BorderStyle.Ridge; 

      for (j = 0; j <= tblCol; j++) 
      { 
       cell = new TableCell(); 
       cell.ID = tbColId + i + j; 
       cell.BorderWidth = 5; 
       cell.BorderStyle = BorderStyle.Ridge; 
       cell.BorderColor = System.Drawing.Color.Azure; 
       for (j = 0; j <= 0; j++) 
       { 
        Label lbl = new Label(); 
        lbl.ID = "lblCCRow" + i + "Col" + j; 
        lbl.Text = "CC NO. " + i + " "; 
        lbl.Width = 100; 
        // Add the control to the TableCell 
        cell.Controls.Add(lbl); 
       } 
       for (j = 1; j <= 1; j++) 
       { 
        Label lbl = new Label(); 
        lbl.ID = "lblRow" + i + "Col" + j; 
        lbl.Width = 100; 
        lbl.Text = Convert.ToString(DateTime.Now.Day) + "/" + Convert.ToString(DateTime.Now.Month) + "/" + Convert.ToString(DateTime.Now.Year); 
        // Add the control to the TableCell 
        cell.Controls.Add(lbl); 
       } 
       for (j = 2; j <= 7; j++) 
       { 
        tb = new TextBox(); 
        tb.Width = 100; 
        tb.ID = "txtBoxRow" + i + "Col" + j; 

        tb.Text = ""; 
        // Add the control to the TableCell 
        cell.Controls.Add(tb); 
       } 
       for (j = 8; j <= 8; j++) 
       { 
        fileUp = new FileUpload(); 
        //m = i; n = j; 
        fileUp.ID = filename + i + j; 
        fileUp.Width = 220; 
        cell.Controls.Add(fileUp); 
        cmdArg = fileUp.ID; 

        UpLdButton = new Button(); 
        UpLdButton.Width = 100; 
        UpLdButton.Text = "Upload" + i + "Col" + j; 
        UpLdButton.ID = UpLdButton.Text; 
        UpLdButton.CommandArgument = cmdArg; 
        cell.Controls.Add(UpLdButton); 

        UpLdButton.Click += new EventHandler(UpLdButton_Click); 
       } 
       for (j = 9; j <= 9; j++) 
       { 
        Label lbl = new Label(); 
        lbl.ID = "lblRow" + i + "Col" + j; 
        lbl.Text = "[ Status ]"; 
        lbl.Width = 100; 
        // Add the control to the TableCell 
        cell.Controls.Add(lbl); 
       } 
       row.Cells.Add(cell); 
      } 

      // Add the TableRow to the Table 
      table.Rows.Add(row); 
     } //outer for-loop end 

     for (i = 0; i < 1; i++) 
     { 
      rrow = new TableRow(); 
      rrow.ID = "ResultRow"; 
      rrow.BorderStyle = BorderStyle.Ridge; 

      for (j = 0; j <= tblCol; j++) 
      { 
       rcell = new TableCell(); 
       rcell.ID = "resultCol" + j; 
       rcell.BorderWidth = 5; 
       rcell.BorderStyle = BorderStyle.Ridge; 
       rcell.BorderColor = System.Drawing.Color.Azure; 
       for (j = 0; j <= 0; j++) 
       { 
        Label lbl = new Label(); 
        //lbl.ID = "lblCCRow" + i + "Col" + j; 
        lbl.Text = "<b>Total</b>"; 
        lbl.Width = 100; 
        // Add the control to the TableCell 
        rcell.Controls.Add(lbl); 
       } 
       for (j = 1; j <= 1; j++) 
       { 
        Label lbl = new Label(); 
        //lbl.ID = "lblRow" + i + "Col" + j; 
        lbl.Width = 100; 
        lbl.Text = Convert.ToString(DateTime.Now.Day) + "/" + Convert.ToString(DateTime.Now.Month) + "/" + Convert.ToString(DateTime.Now.Year); 
        // Add the control to the TableCell 
        rcell.Controls.Add(lbl); 
       } 
       for (j = 2; j <= 7; j++) 
       { 
        tbr = new TextBox(); 
        tbr.Width = 100; 
        tbr.ID = "txtResult" + j; 

        tbr.Text = tbr.ID; 
        // Add the control to the TableCell 
        rcell.Controls.Add(tbr); 
       } 
       for (j = 8; j <= 8; j++) 
       { 
        Button btnCal = new Button(); 
        btnCal.Width = 100; 
        btnCal.Text = "Calculate"; 
        btnCal.ID = btnCal.Text; 
        txtArgs = tb.ID; 
        btnCal.CommandArgument = txtArgs; 
        Label1.Text = txtArgs; 
        rcell.Controls.Add(btnCal); 
        btnCal.Click += new EventHandler(btnCal_Click); 
       } 

       rrow.Cells.Add(rcell); 
      } 

      // Add the TableRow to the Table 
      table.Rows.Add(rrow); 
     } 

     //flag seetting 
     CTflag = true; 
     ViewState["dynamictable"] = true; 
    } 
} 
void btnCal_Click(object sender, EventArgs e) 
{ 
    //Calculation part goes here   
} 
protected override void LoadViewState(object earlierState) 
{ 
    base.LoadViewState(earlierState); 
    if (ViewState["dynamictable"] == null) 
     CreateDynamicTable(); 
} 

void UpLdButton_Click(object sender, EventArgs e) 
{ 
    Button btnUpLD = sender as Button; 
    btnUpLD = (Button)this.FindControl(Form.ID).FindControl(table.ID).FindControl(row.ID).FindControl(cell.ID).FindControl(UpLdButton.ID); 
    //btnUpLD.Text = Convert.ToString((Button)this.FindControl(Form.ID).FindControl(table.ID).FindControl(row.ID).FindControl(cell.ID).FindControl(UpLdButton.ID)); 
    //FileUpload fileupld = new FileUpload(); 
    fileUp = (FileUpload)this.FindControl(Form.ID).FindControl(table.ID).FindControl(row.ID).FindControl(cell.ID).FindControl(fileUp.ID); 
    string args = ((Button)sender).CommandArgument; 
    lblTest.Text = args; 
} 

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    //CreateDynamicTable(); 
    Label1.Text = "Index Changed!!!"; 
} 

}

:

<asp:Label ID="lblTest" runat="server"></asp:Label> 
    &nbsp;<asp:Label ID="Label1" runat="server"></asp:Label> 
    <br /> 
    <br /> 
    Select CC No :&nbsp; 
    <asp:DropDownList ID="DropDownList1" runat="server" 
     onselectedindexchanged="DropDownList1_SelectedIndexChanged" Width="100px"> 
     <asp:ListItem>2</asp:ListItem> 
     <asp:ListItem>3</asp:ListItem> 
     <asp:ListItem>4</asp:ListItem> 
     <asp:ListItem>5</asp:ListItem> 
     <asp:ListItem>6</asp:ListItem> 
    </asp:DropDownList> 
&nbsp;<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Create Table" 
     style="height: 26px" Width="100px" /> 

코드 aspx.cs 파일에 동적 테이블 N 텍스트 상자에 대해 이렇게되면 다음과 같이에서 .aspx 파일의

코드입니다 다음 코드는 내가 시도한 것입니다 ... 그것은 잘못되었습니다.

텍스트 상자에 ID의 m은 마지막 텍스트 상자의 ID를 받고 난

으로 반복 트로프 텍스트 상자 아이디의

void btnCal_Click(object sender, EventArgs e) 
{ 
    Button btnCalculate = sender as Button; 
    TextBox txtFields; // Resultant txtField   
    string[,] txtNames = new string[tblRow,6]; 

    double TotAmt = new double(); 
    double NoOfRect = new double(); 
    double Energy = new double(); 
    double New1 = new double(); 
    double Theft = new double(); 
    double Misc = new double(); 

    TotAmt = 0; NoOfRect = 0; Energy = 0; New1 = 0; Theft = 0; Misc = 0; 



    for (int i = 0; i < tblRow; i++) 
    { 
     for (int j = 2; j <= 7; j++) 
     { 
      txtRes = new TextBox(); 
      //txtFields = (TextBox)this.FindControl(Form.ID).FindControl(table.ID).FindControl(row.ID).FindControl(cell.ID).FindControl(tb.ID); 
      //txtFields.Text = txtFields.ID; 
      //txtNames[i,j] = txtFields.Text; 
      if (j == 2) 
      { 
       txtFields = (TextBox)this.FindControl(Form.ID).FindControl(table.ID).FindControl(row.ID).FindControl(cell.ID).FindControl(tb.ID); 
       txtNames[i, j] = txtFields.Text; 
       TotAmt+= Convert.ToDouble(txtNames[i, j]); 
       txtRes = (TextBox)this.FindControl(Form.ID).FindControl(table.ID).FindControl(rrow.ID).FindControl(rcell.ID).FindControl(tbr.ID); 
       txtRes.Text = Convert.ToString(TotAmt); 
      } 
      else if (j == 3) 
      { 
       txtFields = (TextBox)this.FindControl(Form.ID).FindControl(table.ID).FindControl(row.ID).FindControl(cell.ID).FindControl(tb.ID); 
       txtNames[i, j] = txtFields.Text; 
       NoOfRect+= Convert.ToDouble(txtNames[i, j]); 
       txtRes = (TextBox)this.FindControl(Form.ID).FindControl(table.ID).FindControl(rrow.ID).FindControl(rcell.ID).FindControl(tbr.ID); 
       txtRes.Text = Convert.ToString(NoOfRect); 
      } 
      else if (j == 4) 
      { 
       txtFields = (TextBox)this.FindControl(Form.ID).FindControl(table.ID).FindControl(row.ID).FindControl(cell.ID).FindControl(tb.ID); 
       txtNames[i, j] = txtFields.Text; 
       Energy += Convert.ToDouble(txtNames[i, j]); 
       txtRes = (TextBox)this.FindControl(Form.ID).FindControl(table.ID).FindControl(rrow.ID).FindControl(rcell.ID).FindControl(tbr.ID); 
       txtRes.Text = Convert.ToString(Energy); 
      } 
      else if (j == 5) 
      { 
       txtFields = (TextBox)this.FindControl(Form.ID).FindControl(table.ID).FindControl(row.ID).FindControl(cell.ID).FindControl(tb.ID); 
       txtNames[i, j] = txtFields.Text; 
       New1 += Convert.ToDouble(txtNames[i, j]); 
       txtRes = (TextBox)this.FindControl(Form.ID).FindControl(table.ID).FindControl(rrow.ID).FindControl(rcell.ID).FindControl(tbr.ID); 
       txtRes.Text = Convert.ToString(New1); 
      } 
      else if (j == 6) 
      { 
       txtFields = (TextBox)this.FindControl(Form.ID).FindControl(table.ID).FindControl(row.ID).FindControl(cell.ID).FindControl(tb.ID); 
       txtNames[i, j] = txtFields.Text; 
       Theft += Convert.ToDouble(txtNames[i, j]); 
       txtRes = (TextBox)this.FindControl(Form.ID).FindControl(table.ID).FindControl(rrow.ID).FindControl(rcell.ID).FindControl(tbr.ID); 
       txtRes.Text = Convert.ToString(Theft); 
      } 
      else if (j == 7) 
      { 
       txtFields = (TextBox)this.FindControl(Form.ID).FindControl(table.ID).FindControl(row.ID).FindControl(cell.ID).FindControl(tb.ID); 
       txtNames[i, j] = txtFields.Text; 
       Misc += Convert.ToDouble(txtNames[i, j]); 
       txtRes = (TextBox)this.FindControl(Form.ID).FindControl(table.ID).FindControl(rrow.ID).FindControl(rcell.ID).FindControl(tbr.ID); 
       txtRes.Text = Convert.ToString(Misc); 
      } 
      else 
      { 
       Label1.Text = Convert.ToString("Value of J : " +j); 
      } 
      //txtRes = (TextBox)this.FindControl(Form.ID).FindControl(table.ID).FindControl(rrow.ID).FindControl(rcell.ID).FindControl(tbr.ID); 
      //txtRes.Text = Convert.ToString(results[i, j]); 
     } 
    } 

Plz은에 unale 오전 읽는 동안 그것을 할 나를 올바른 방법을 제안한다.

답변

0

이것은 유용한 기능 일 수 있습니다. Function source

public static Control FindControlRecursive(Control root, string id) 
    { 
     if (root.ID == id) 
     { 
      return root; 
     } 

     foreach (Control c in root.Controls) 
     { 
      Control t = FindControlRecursive(c, id); 
      if (t != null) 
      { 
       return t; 
      } 
     } 

     return null; 
    } 

그런 다음이 텍스트 상자를 반복하면됩니다.

for (int i = 0; i < tblRow; i++) 
     { 
      for (int j = 2; j <= 7; j++) 
      { 
       TextBox tb = FindControlRecursive(this, string.Format("txtBoxRow{0}Col{1}", i, j)); 
       switch(j) 
       { 
        case 2: 
         TotAmt += Convert.ToDouble(tb.Text); 
         break; 
        case 3: 
         break; 
        default: 
         Label1.Text = Convert.ToString("Value of J : " + j); 
       } 
      } 
     } 
+0

굉장한 사람입니다! 대단히 감사합니다. –