动态文本框事件TextChanged未触发

时间:2015-08-13 00:24:09

标签: c# asp.net

我遇到动态文本框事件的问题。我得到了其他动态文本框及其textchanged事件,其他的工作正常,但是这个事件永远不会进入事件,属性AutoPostBack = true,EnabledViewState,EnabledViewTheming,它也进入UpdatePanel,我创建了一个动态触发。

这是我的代码:

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);

这是我的活动代码

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

但永远不要进入它。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

当我们在ASP.Net中创建控件时,可以在表单加载时或之后创建控件。但是如果你想要控件的事件。您应该在 onInit 方法中对其进行初始化。然后它会正常工作。 如果我想创建动态文本框及其事件。并希望在面板中添加

请参阅以下代码。

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)
        {

        }

另外请确保控件的ID正确。必须是interger,下划线和字母数字。 ex t001,t_1等

但不应该空间。

相关问题