anthro
anthro

Reputation: 1

Dynamic events added to dynamic textboxes not triggering

I'm wanting to add some dynamic textboxs to a table and have them trigger an event. I'm having no problem creating the textboxs, or accessing the information about them, but they do not want to trigger the event.

    List<string> txtNames = new List<string>();
    txtNames.Add("txtId");
    txtNames.Add("txtName");
    txtNames.Add("txtQty");
    txtNames.Add("txtUnitPrice");
    txtNames.Add("txtExtendedPrice");

    Panel1.Controls.Add(new LiteralControl("\t<tr>\n"));
    TextBox txt;
    foreach (string s in txtNames)
    {
        txt = new TextBox();
        txt.CopyBaseAttributes(Textbox1);
        txt.Text = "";
        txt.BorderWidth = 0;
        txt.ID = s + row;

        Panel1.Controls.Add(new LiteralControl("\t\t<td>"));
        Panel1.Controls.Add(txt);
        Panel1.Controls.Add(new LiteralControl("</td>\n"));

        txt.AutoPostBack = true;
        txt.TextChanged += new EventHandler(txt_TextChanged);

        txt.Text = s;
        count++;
    }
    Panel1.Controls.Add(new LiteralControl("\t</tr>\n"));
    row++;

(the panel is inside a html table)

This code is in a function that is called when the page is not post back or when a button is clicked to add a row of textboxes.

The text boxes are saved to a session then reloaded on page_load.

I've tried making an test textbox that was not created in a for loop and that one does trigger the text change event.

Edit: I tried re-adding the text change event when loaded the textboxes from the saved session and now they are triggering the function.

Upvotes: 0

Views: 1465

Answers (2)

jbl
jbl

Reputation: 15413

You should place you method call inside OnInit instead of pageload, and set the Text property only when not post back

   protected override void OnInit( EventArgs e)
    {
        base.OnInit(e);
        int row = 0;
        int count = 0;
        List<string> txtNames = new List<string>();
        txtNames.Add("txtId");
        txtNames.Add("txtName");
        txtNames.Add("txtQty");
        txtNames.Add("txtUnitPrice");
        txtNames.Add("txtExtendedPrice");

        Panel1.Controls.Add(new LiteralControl("\t<tr>\n"));
        TextBox txt;
        foreach (string s in txtNames)
        {
            txt = new TextBox();
            //txt.CopyBaseAttributes(Textbox1);
            txt.BorderWidth = 0;
            txt.ID = s + row;

            Panel1.Controls.Add(new LiteralControl("\t\t<td>"));
            Panel1.Controls.Add(txt);
            Panel1.Controls.Add(new LiteralControl("</td>\n"));

            txt.AutoPostBack = true;
            txt.TextChanged += (sndr, evt) => { Response.Write(((Control)sndr).ID + " --- " + ((TextBox)sndr).Text); };

            if(!IsPostBack)
                txt.Text = s;
            count++;
        }
        Panel1.Controls.Add(new LiteralControl("\t</tr>\n"));
        row++;

    }

Upvotes: 1

bodee
bodee

Reputation: 2674

The controls and events should be recreated in Page_Load, because the dynamic controls are lost after request.

Upvotes: 0

Related Questions