Reputation: 1
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.
On PageInit, I generate a Table with Headers and one of the cells having my DropDownList.
//Essentially an ArrayList with "row" information (it uses a Field class with
//several parameters, each corresponding to 1 column in the table
GenericFieldsCollection data = new GenericFieldsCollection();
data.FillFieldList();
Table TableToEdit = new Table();
TableHeaderCell Header_FieldName = new TableHeaderCell();
(...)
foreach (GenericField row in data.FieldList)
{
(...)
//The cell with the DDL:
TableCell Column_GraphName = new TableCell();
Column_GraphName.Controls.Add(GenerateGraphSelectionDropdown());
(...)
}
Obviously, if I do not generate it here, the Click events won't work.
This procedure only queries the database with a Select, no updates are made.
After the first (!PostBack) call, subsequent calls can be triggered by changing the DropDownList's index (it has AutoPostBack=true).
So, my PageInit runs again, as expected. And, after Load Number 15 in here happens ( OnXXX (control event)), and my database gets updated.
Later in the Cycle (OnPreRender), I tried clearing the Div anchor I'm using for this table of all controls, and call my GenerateFieldCollectionTable() again. Since my update query in my DropDownList's SelectedIndexChanged event has already executed, I expected to see the updated values. However, doing this will show the old value, and wreck the behaviour (I won't be able to change anything and there is no change in the database).
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
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.
Upvotes: 1