OnSpxtChanged事件未在UpdatePanel内部触发 - 两者都是动态创建的

时间:2013-12-21 15:07:49

标签: c#

我想要做的是用文本框填充GridView的列并执行一些函数OnTextChanged。 这是我的代码:

   if (e.Row.RowType == DataControlRowType.DataRow)
    {

        UpdatePanel UP_AmountToBuy = new UpdatePanel();
        UP_AmountToBuy.ContentTemplateContainer.Controls.Clear();
        UP_AmountToBuy.Triggers.Clear();
        UP_AmountToBuy.UpdateMode = UpdatePanelUpdateMode.Conditional;
        UP_AmountToBuy.ChildrenAsTriggers = false;
        UP_AmountToBuy.Attributes["runat"] = "server";

        //Create and add TextBox
        TextBox TB_AmountToBuy = new TextBox();
        TB_AmountToBuy.Text = "0";
        TB_AmountToBuy.TextChanged += new EventHandler(TB_AmountToBuy_TextChanged);
        TB_AmountToBuy.Attributes["OnTextChanged"] = "TB_AmountToBuy_TextChanged";
        TB_AmountToBuy.Attributes["runat"] = "server";
        TB_AmountToBuy.AutoPostBack = true;
        TB_AmountToBuy.ViewStateMode = System.Web.UI.ViewStateMode.Enabled;
        TB_AmountToBuy.ID = "buyID" + count;
        UP_AmountToBuy.ContentTemplateContainer.Controls.Add(TB_AmountToBuy);

        //Create and add AsyncPostBackTrigger
        AsyncPostBackTrigger APBT_trig = new AsyncPostBackTrigger();
        APBT_trig.EventName = "TextChanged";
        APBT_trig.ControlID = TB_AmountToBuy.ID;
        UP_AmountToBuy.Triggers.Add(APBT_trig);

        Label newLBL = new Label();
        newLBL.Text = "123";
        newLBL.Attributes["runat"] = "server";
        UP_AmountToBuy.ContentTemplateContainer.Controls.Add(newLBL);

        e.Row.Cells[5].Controls.Add(UP_AmountToBuy);
        count++;
    }
}

public void TB_AmountToBuy_TextChanged(object sender, EventArgs e)
{
    ((sender as TextBox).Parent.Controls[1] as Label).Text = (sender as TextBox).Text;
}

问题是,OnTextChanged事件永远不会被触发......

2 个答案:

答案 0 :(得分:0)

每次在页面加载时都需要创建动态生成的控件,以便恢复其视图状态,触发事件并从中获取值。就像这样

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


        if (!IsPostBack)
        {
            var dt = new System.Data.DataTable();
            dt.Columns.Add("Col1");
            dt.Rows.Add("Hi");
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        CreateDynamicControles();


    }

    public void CreateDynamicControles()
    {
        var count = 0;
        foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {

                UpdatePanel UP_AmountToBuy = new UpdatePanel();
                UP_AmountToBuy.ContentTemplateContainer.Controls.Clear();
                UP_AmountToBuy.Triggers.Clear();
                UP_AmountToBuy.UpdateMode = UpdatePanelUpdateMode.Conditional;
                UP_AmountToBuy.ChildrenAsTriggers = false;
                UP_AmountToBuy.Attributes["runat"] = "server";

                //Create and add TextBox
                TextBox TB_AmountToBuy = new TextBox();
                TB_AmountToBuy.Text = "0";
                TB_AmountToBuy.TextChanged += new EventHandler(TB_AmountToBuy_TextChanged);
                TB_AmountToBuy.Attributes["OnTextChanged"] = "TB_AmountToBuy_TextChanged";
                TB_AmountToBuy.Attributes["runat"] = "server";
                TB_AmountToBuy.AutoPostBack = true;
                TB_AmountToBuy.ViewStateMode = System.Web.UI.ViewStateMode.Enabled;
                TB_AmountToBuy.ID = "buyID" + count;
                UP_AmountToBuy.ContentTemplateContainer.Controls.Add(TB_AmountToBuy);

                //Create and add AsyncPostBackTrigger
                AsyncPostBackTrigger APBT_trig = new AsyncPostBackTrigger();
                APBT_trig.EventName = "TextChanged";
                APBT_trig.ControlID = TB_AmountToBuy.ID;
                UP_AmountToBuy.Triggers.Add(APBT_trig);

                Label newLBL = new Label();
                newLBL.Text = "123";
                newLBL.Attributes["runat"] = "server";
                UP_AmountToBuy.ContentTemplateContainer.Controls.Add(newLBL);

                row.Cells[0].Controls.Add(UP_AmountToBuy);
                 count++;
            }
        }
    }



    public void TB_AmountToBuy_TextChanged(object sender, EventArgs e)
    {
        ((sender as TextBox).Parent.Controls[1] as Label).Text = (sender as TextBox).Text;
    }
}

答案 1 :(得分:0)

尝试此操作,因为您使用的是UpdatePanel。所以使用以下属性。

设置属性:

EnableViewstate="True"