如何在动态表中处理动态文本框?

时间:2013-03-06 09:16:13

标签: c# asp.net textbox findcontrol dynamic-controls

我希望从相同列中的文本框中向上(添加)值,并在单击“计算”按钮时分别将SUM显示到最后一行文本框 单击“创建表”按钮动态生成所有文本框。

我没有得到如何获取这些文本框的ID。

.aspx文件的代码如下:

    <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文本框,如下所示:

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

}

以下代码是我试过的......但是它有问题。

读取textBox ID时m只获取最后一个textBox ID而且我是unale来通过textBox 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建议我采用正确的方法。

1 个答案:

答案 0 :(得分: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);
                }
            }
        }