Как да обработвам динамични текстови полета в динамична таблица?

Искам да СУМИРАМ (добавя) стойности от текстови полета в едни и същи колони и да покажа СУМ към последния ред текстови полета съответно при щракване върху бутона „Изчисляване“
Всички текстови полета се генерират динамично при щракване върху бутона „Създаване на таблица“.

Не разбирам как да получа идентификаторите на тези текстови полета.

Кодът на .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" />

Кодът изглежда така за текстови полета на динамична таблица n във файла aspx.cs:

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

}

следният код е това, което опитах ... bt той е дефектен.

докато чета ID на текстовото поле, получавам само последното ID на текстовото поле и не мога да

повтарям 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]);
        }
    }

Моля, предложете ми правилния начин да го направя.


person Nishant SB    schedule 06.03.2013    source източник


Отговори (1)


Това може да е полезна функция. Източник на функция

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);
                }
            }
        }
person Stephen    schedule 06.03.2013