简体   繁体   中英

TextChanged event on textbox in dynamically added user control not firing

I am dynamically adding a number of user controls, which have a textbox. When the text in the textbox is changed, I am trying to fire an event.

My code is as such:

Default.aspx:

<asp:Panel ID="pnlControls" runat="server"/>

Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    MyControl control = (MyControl)LoadControl("Controls/MyControl.ascx");
    control.NameField = "Test Name";
    pnlControls.Controls.Add(control);
}

MyControl.ascx

<asp:TextBox ID="txtNameField" runat="server" OnTextChanged="txtNameField_TextChanged" AutoPostBack="true"/>

MyControl.ascx.cs

public string NameField;

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostback)
    {
        txtNameField.Text = NameField;
    }
}

protected void txtNameField_TextChanged(object sender, EventArgs e)
{
    NameField = txtNameField.Text;
}

This is a simplified version of the code, which contains all relevant parts that are necessary to make the functionality work as required (as far as I know)

However, the txtNameField_TextChanged function is never executed. How can I make the function execute when the txtNameField text is changed?

Try to assign an ID to the UserControl :

MyControl control = (MyControl)LoadControl("Controls/MyControl.ascx");
control.ID = "MyUserControl";
control.NameField = "Test Name";
pnlControls.Controls.Add(control);

Controls must have the same ID at postback to be able to fire events or to load their viewstate.

As an aside, you don't need to assign txtNameField.Text to your field in the event. Just use a property that gets/sets txtNameField.Text . Then you have always the correct value and it's also retained across postbacks automatically.

So instead of this:

public string NameField;

protected void txtNameField_TextChanged(object sender, EventArgs e)
{
    NameField = txtNameField.Text;
}

This:

public string NameField 
{
    get
    {
        return txtNameField.Text;
    }
    // maybe you want to make it public
    private set 
    {
        txtNameField.Text = value;
    }
}

You need to add txtNameField to your textChanged-Event.

MyControl:

this.TextChanged += new EventHandler(txtNameField_TextChanged);

At least I think that is what you are trying to do, because I can't see this Line anywhere in your code.

My guess is that somewhere the binding of your click event is getting lost. Please try to add your control dynamically earlier than the Page_Load , the best place to do it is in Page_Init where other controls are as well getting initialized. (at that point all other controls in your page are actually initialized)

Please check the ASP.NET Page Life Cycle Overview where it is clearly indicated that dynamic controls should be added during the preinit stage. And here is a link to the LoadControl method you are using where the control is as well added dynamically in the init stage in the code sample.

Could you try & test with debugger attached.

I copied your code & executed, for me "OnTextChanged" event is firing. Even I have placed the textbox inside user control which i am loading proframmatically.

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