简体   繁体   中英

Dynamic Textbox event TextChanged is not firing

I have a problem with a Dynamic Textbox event. I got other dynamic textbox with their textchanged events, the others work good, but this one never get into the event, the property AutoPostBack = true, EnabledViewState too, EnabledViewTheming too, and it's into an UpdatePanel and I create a Dynamic Trigger.

This is my code:

TextBox DescUnit = new TextBox();
DescUnit.ID = "DescUnit_txt" + (No).ToString();
DescUnit.Text = "0.0";
DescUnit.TextChanged += new EventHandler(DescUnit_TextChanged);
DescUnit.AutoPostBack = true;
DescUnit.EnableViewState = true;
DescUnit.EnableTheming = true;
Trgr = new PostBackTrigger();
Trgr.ControlID = DescUnit.ID;
UpdatePanel1.Triggers.Add(Trgr);
Table.Rows[i - 1].Cells[3].Controls.Add(DescUnit);

And this is the code of my Event

protected void DescUnit_TextChanged(object sender, EventArgs e)
{
    Descuento_Row.Visible = true;
    int i = 1;
    foreach (HtmlTableRow Row in Tab.Rows)
    {
        if (Row.ID != null && String.Compare(Row.ID.Substring(0, 6), "TRDet_") == 0)
        {
            Detalle = (HtmlTable)(Row.Cells[0].Controls[0]);
            if (sender.Equals(Detalle.Rows[1].Cells[3].Controls[0]))
            {
                TextBox Cantidad = new TextBox();
                Clonar(Tab.Rows[i].Cells[1].Controls[0], Cantidad);
                TextBox Precio = new TextBox();
                Clonar(Tab.Rows[i].Cells[4].Controls[0], Precio);
                TextBox DescUnit = new TextBox();
                Clonar(Detalle.Rows[1].Cells[3].Controls[0], DescUnit);
                TextBox ImpDesc = new TextBox();
                Clonar(Detalle.Rows[2].Cells[4].Controls[0], ImpDesc);

                ImpDesc_txt.Text = ((Convert.ToDouble(ImpDesc_txt.Text) - Convert.ToDouble(ImpDesc.Text)) + (Convert.ToDouble((Convert.ToDouble(DescUnit.Text) / 100)) * (Convert.ToDouble(Precio.Text) * Convert.ToInt32(Cantidad.Text)))).ToString();
                ImpDesc.Text = (Convert.ToDouble((Convert.ToDouble(DescUnit.Text) / 100)) * (Convert.ToDouble(Precio.Text) * Convert.ToInt32(Cantidad.Text))).ToString();

                Detalle.Rows[2].Cells[4].Controls.Clear();
                Detalle.Rows[2].Cells[4].Controls.Add(ImpDesc);
            }

            i = i + 2;
        }
    }
}

But never get into it. Can anyone help me?

When ever we are creating controls in ASP.Net the controls can be create on form load or after it. But if you want events of the controls. you should initialize it in onInit Method. Then It will work properly. If suppose I want to create dynamic textbox and its event. and want to add it in panel

Please refer following code.

protected override void OnInit(EventArgs e)
        {
            TextBox t = new TextBox();
            t.ID = "t01";
            t.TextChanged += t_TextChanged;
            t.AutoPostBack = true;
            Panel1.Controls.Add(t);
        }

      protected  void t_TextChanged(object sender, EventArgs e)
        {

        }

Also please be sure that ID of the control is properly. Must interger,underscore and alphanumeric. ex t001, t_1 etc

But should not space.

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