Cloud
Cloud

Reputation: 1

ASP Lifecycle with dynamic DropDownList and Postbacks

Good afternoon.

I am attempting to create a DropDownList populated in 2 ways:

The DropDownList's SelectedIndexChanged takes care of updating the database for me.

Note that the control is working already, but I think I'm not understanding the lifecycle correctly and would like some clarification.

Obviously, if I do not generate it here, the Click events won't work.

After a bit of testing, I found out that Page_PreRender having this line is the issue:

        ------> //TableToEditGraphFields.Controls.Clear();
        GenerateFieldCollectionTable();

Clearing the controls of TableToEditGraphFields, an action that happens AFTER CLICK EVENTS (since Control.OnPreRender is #22 and OnSelectedIndexChanged is #15), means that on the next PostBack the Controls won't trigger click events, as they've been cleared. Is there a part of the cycle where I can process the Click event, rebuild the controls, and still have them clickable on the next PostBack?

Thank you in advance.

EDIT: Code that generates the table:

            GenericFieldsCollection data = new GenericFieldsCollection();
        data.FillFieldList();

        Table TableToEdit = new Table();

        TableToEdit.CssClass = "table";

        TableRow Headers = new TableRow();

        TableHeaderCell Header_FieldName = new TableHeaderCell();
        Header_FieldName.Text = "Field Name";
        Headers.Cells.Add(Header_FieldName);

        //Repeat for each header

        TableToEdit.Rows.Add(Headers);

        foreach (GenericField row in data.FieldList)
        {

            TableRow currentRow = new TableRow();

            //Repeat for each column; example with the "Order" column

            TableCell Column_Order = new TableCell();

            TextBox editableOrder = new TextBox();
            editableOrder.Attributes.Add("FieldName", row.Name);
            editableOrder.Attributes.Add("FieldTable", row.Table);
            editableOrder.AutoPostBack = true;
            editableOrder.TextChanged += EditableOrder_TextChanged;
            editableOrder.Text = row.Order.ToString();
            Column_Order.Controls.Add(editableOrder);

            currentRow.Cells.Add(Column_Order);

            TableToEdit.Rows.Add(currentRow);
        }

        TableToEditGraphFields.Controls.Add(TableToEdit);

Upvotes: 0

Views: 279

Answers (1)

user4628051
user4628051

Reputation:

" I'm not understanding the lifecycle correctly and would like some clarification."

How does the life cycle work?

As you can see in the picture below. This is how the life cycle is working. All the information is from codedisplay There is a lot more information for you to read.

enter image description here

Upvotes: 1

Related Questions