简体   繁体   中英

Dynamic textbox values to database

I am trying to insert values from dynamic textbox to database using Entity Framework in Web Forms. On one click it make a table row with three column and every column has one textbox. Every another click is one row more.

public static int rowCnt = 0;
    protected void BtnAddNewBuildItem_Click(object sender, EventArgs e)
    {
        // Current row count.
        int rowCtr;
        // Total number of cells per row (columns).
        int cellCtr;
        // Current cell counter
        int cellCnt;

        rowCnt = rowCnt + 1;
        cellCnt = 3;

        for (rowCtr = 1; rowCtr <= rowCnt; rowCtr++)
        {
            // Create new row and add it to the table.
            TableRow tRow = new TableRow();
            TableBuildItems.Rows.Add(tRow);

            for (cellCtr = 1; cellCtr <= cellCnt; cellCtr++)
            {
                // Create a new cell and add it to the row.
                TableCell tCell = new TableCell();
                TextBox tb = new TextBox();

                // Set a unique ID for each TextBox added
                tb.ID = "txtBuisniesItem_Row" + rowCtr + "Cell" + cellCtr;
                // Add the control to the TableCell
                tCell.Controls.Add(tb);

                tRow.Cells.Add(tCell);

            }
        }
    }

Then, on another button click i tried to insert one row values to database, but allways is empty string.

using (VODOMONTEntities context = new VODOMONTEntities())
            {
                BuildItem bi = new BuildItem();

                for (int i = 0; i < rowCnt; i++)
                {  
                    TextBox tb1 = new TextBox();                        
                    tb1.ID = "txtBuisniesItem_Row" + (i + 1).ToString() + "Cell" + 1;
                    bi.Name = tb1.Text;   //there is a empty string allways
                    context.BuildItems.Add(bi);   
                }
                context.SaveChanges();                 
            }

So, my question is how to insert a dynamic texbox values to database, like in this case? Thank you!

 protected void CreateNewBuild_Click(object sender, EventArgs e)
    {           
        using (VODOMONTEntities context = new VODOMONTEntities())
        {
            Build b = new Build();
            BuildItem bi = new BuildItem();

            b.BuildSubject = txbSubject.Text;
            b.BuildNumber = txbBuildNumber.Text;
            b.City = txbCityBuild.Text;
            // b.BuildDate = txbBuildDate.Text;
            b.ClientId = Int32.Parse(ddlClient.SelectedValue);
            context.Builds.Add(b);                    

            for (int i = 0; i < (RowIndexNumber-1)/3; i++)
            {
                TextBox[] tb = new TextBox[RowIndexNumber];
                tb[i*3] = new TextBox();
                tb[i*3] = (TextBox)FindControl("txtBuisniesItem_Row" + (i * 3 + 1).ToString());
                bi.Name = tb[i*3].Text;

                tb[i*3+1] = new TextBox();
                tb[i*3+1] = (TextBox)FindControl("txtBuisniesItem_Row" + (i * 3 + 2).ToString());               
                bi.Quantity = Convert.ToInt32(tb[i * 3 + 1].Text);

                tb[i*3+2] = new TextBox();
                tb[i*3+2] = (TextBox)FindControl("txtBuisniesItem_Row" + (i * 3 + 3).ToString());
                bi.Price = decimal.Parse(tb[i * 3 + 2].Text.ToString());                                         

                bi.Build = b;
                context.BuildItems.Add(bi);
                context.SaveChanges();

            }
        }            
    }

    public int RowIndexNumber = 1;
    protected void Page_PreInit(object sender, EventArgs e)
    {
        List<string> keys = Request.Form.AllKeys.Where(key => key.Contains("txtBuisniesItem_Row")).ToList();

        foreach (string key in keys)
        {

                this.CreateTextBox("txtBuisniesItem_Row" + RowIndexNumber);
            if (RowIndexNumber % 3 == 0)
            {
                Literal lt = new Literal();
                lt.Text = "<br />";
                pnlTextBoxes.Controls.Add(lt);
            }
            RowIndexNumber++;
        }
    }

    protected void BtnAddNewBuildItem_Click(object sender, EventArgs e)
    {

        int index = pnlTextBoxes.Controls.OfType<TextBox>().ToList().Count + 1;
        this.CreateTextBox("txtBuisniesItem_Row" + index);
        this.CreateTextBox("txtBuisniesItem_Row" + (index + 1).ToString());
        this.CreateTextBox("txtBuisniesItem_Row" + (index + 2).ToString());
        Literal lt = new Literal();
        lt.Text = "<br />";
        pnlTextBoxes.Controls.Add(lt);
    }
    private void CreateTextBox(string id)
    {
        TextBox txt = new TextBox();
        txt.ID = id;
        txt.Attributes.Add("runat", "Server");
        pnlTextBoxes.Controls.Add(txt);

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM