0

Hello everybody and thanks in advance,

I'm dynamically creating some textboxes inside a panel control which is placed inside a formview when I click on a button this way...

    protected void Muestra_Precio_Stock_BT_Click(object sender, EventArgs e)
    {
        CheckBoxList FormatosCL = (CheckBoxList)FormViewDiscos.FindControl("FormatosCL");
        Panel Precio_Stock_PN = (Panel)FormViewDiscos.FindControl("CB_Precio_Stock_PN");

        Label NuevoPrecioLB = null;
        TextBox NuevoPrecioTB = null;
        Label NuevoStockLB = null;
        TextBox NuevoStockTB = null;

        foreach (ListItem item in FormatosCL.Items)
        {
            if(item.Selected)
            {
                NuevoPrecioLB = new Label();
                NuevoPrecioTB = new TextBox();
                NuevoStockLB = new Label();
                NuevoStockTB = new TextBox();

                NuevoPrecioLB.ID = "NuevoPrecioLB_" + FormatosCL.Items.IndexOf(item);
                NuevoPrecioLB.Text = "PRECIO " + item.Text + ": ";
                NuevoPrecioTB.ID = "NuevoPrecioTB_" + FormatosCL.Items.IndexOf(item);

                NuevoStockLB.ID = "NuevoStockLB_" + FormatosCL.Items.IndexOf(item);
                NuevoStockLB.Text = " STOCK " + item.Text + ": ";
                NuevoStockTB.ID = "NuevoStockTB_" + FormatosCL.Items.IndexOf(item);

                Precio_Stock_PN.Controls.Add(NuevoPrecioLB);
                Precio_Stock_PN.Controls.Add(NuevoPrecioTB);
                Precio_Stock_PN.Controls.Add(NuevoStockLB);
                Precio_Stock_PN.Controls.Add(NuevoStockTB);

                Precio_Stock_PN.Controls.Add(new LiteralControl("<br />"));
            }
        }
    }

Well, it works well, when I run the project the textboxes and labels are created as expected, I can see them and write into the textboxes. I`ve put the same code into Page_Load event and now I can get the created controls (that was my first problem) when I click the INSERT button of the formview. The problem is that, in the following code...

    protected void InsertButton_Click(object sender, EventArgs e)
    {
        TextBox DiscIdTB = (TextBox)FormViewDiscos.FindControl("DiscIdTB");
        TextBox CaratulaTB = (TextBox)FormViewDiscos.FindControl("CaratulaTB");
        CheckBoxList CancionesCL = (CheckBoxList)FormViewDiscos.FindControl("CancionesCL");
        CheckBoxList FormatosCL = (CheckBoxList)FormViewDiscos.FindControl("FormatosCL");

        using (SqlConnection conexion = new SqlConnection(ConfigurationManager.ConnectionStrings["ConexionBBDD-Discos"].ConnectionString))
        {
            conexion.Open();

            cmd.Connection = conexion;

            cmd.CommandText = "INSERT INTO DISCOS_FORMATOS (ID_DISCO, ID_FORMATO, PRECIO, STOCK) VALUES ((SELECT MAX(ID_DISCO) FROM DISCOS), @IdFormato, 0, 0)";

            foreach (ListItem item in FormatosCL.Items)
            {
                if (item.Selected)
                {
                    Panel Precio_Stock_PN = (Panel)FormViewDiscos.FindControl("CB_Precio_Stock_PN");

                    string control = "NuevoPrecioTB_" + (FormatosCL.Items.IndexOf(item));
                    string control2 = "NuevoStockTB_" + (FormatosCL.Items.IndexOf(item));

                    TextBox Precio = (TextBox)Precio_Stock_PN.FindControl(control);
                    TextBox Stock = (TextBox)Precio_Stock_PN.FindControl(control2);

                    cmd.Parameters.Clear();
                    cmd.Parameters.AddWithValue("@IdFormato", item.Value);
                    cmd.Parameters.AddWithValue("@IdPrecio", Convert.ToInt32(Precio.Text));
                    cmd.Parameters.AddWithValue("@IdStock", Convert.ToInt32(Stock.Text));

                    cmd.ExecuteNonQuery();

                }

            }

            conexion.Close();
            cmd.Dispose();
        }

    }

when the execution reaches these lines, dynamically created textboxes lost their values, returning always 0...

     cmd.Parameters.AddWithValue("@IdPrecio", Convert.ToInt32(Precio.Text));
     cmd.Parameters.AddWithValue("@IdStock", Convert.ToInt32(Stock.Text));

How can I preserve their user typed values?

Mazinger
  • 633
  • 1
  • 6
  • 12
  • A quick look on the related column on the right shows at least 5 or 6 duplicates. Did you check those questions? In what they are not explaining and solving your problem? – Steve Mar 27 '16 at 21:17
  • Hi, Steve, I´ve read several, but still not solving my problem. – Mazinger Mar 27 '16 at 21:24
  • Hi usr, I've rewritten my question, now it's about related but different problem, and it has nothing to do with the suggested duplicated question.Thanks! – Mazinger Mar 27 '16 at 23:14
  • Please, can you reopen my question? Thanks. – Mazinger Mar 27 '16 at 23:35
  • OK, I suggest that you ask a new question and delete this one because that's a different question. This one is doomed, nobody's going to look at it again. You'll not get good help. – usr Mar 28 '16 at 10:54
  • Ok, thank you very much! – Mazinger Mar 28 '16 at 10:55

0 Answers0