• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

ASP.NET - Creating Controls in Code

clamum

Lifer
I am dynamically creating table rows, cells, and controls (dropdown lists and checkboxes) in ASP.NET. I have a standard "<table id='tblName' runat='server'></table>" tag in the aspx and in the code-behind I dynamically create rows depending on a count. In those rows are two columns, one for a dropdown list and one for a checkbox. In a final dynamic row, there is a button being created that adds a new row.

My problem is that I'll first add a new row (the table starts out blank except for the row with the button), select an item in the dropdown list and/or check the checkbox, and then click the button to add another row. Clicking the button adds the new row fine, but the dropdown list and checkbox in the first row are then reset.

I thought the values of controls were stored in the ViewState but perhaps I'm misunderstanding. I should also note that this is being done in an UpdatePanel to avoid full page postbacks. Does anyone have any insight as to what's going on and why the values do not propagate across button clicks? Thanks!

The code is roughly:

C#:

void Page_Load() {
....int[] listOfRowNumbers = GetFromDB();
....BuildTable();
}

void BuildTable() {
....foreach (int i in listOfRowNumbers) {
........HtmlTableRow row = new HtmlTableRow();
........// create cell here
........DropDownList ddl = new DropDownList();
........ddl.DataSource = someCollection();
........ddl.DataBind();
........// create checkbox
........// create button, assign click handler
........// add controls to cell, cell to row, row to table
....}
}

void Button_Click() {
....// add new row number to list of row numbers, save to db
....BuildTable();

ASPX:

<asp:UpdatePanel>
....<ContentTemplate>
........<table id="tblTable" runat="server"></table>
....</ContentTemplate>
</asp:UpdatePanel>
 
Though when I add a button outside of the UpdatePanel; then add several rows, then select the values in the dropdowns/check the checkboxes and click the button outside of the panel, the values are re-displayed on postback. Clicking the button inside the panel, in the table, wipes the values out. I guess I still don't understand ViewState or the Page Lifecycle as much as I would like to.
 
when you rebind a dropdown list, its data / viewstate is reset.

in your page_load

wrap your code in

if not ispostback then {}
 
Dynamic controls need to be created in Page_Init.
You also need to keep track of the previously added controls yourself and give them IDs. The system will not re-create dynamic controls for you (with some exceptions).

I would recommend dropping the Update panel until you get the basic thing working then add it back.
 
Jackdruid: I did have it in an !IsPostback check but after clicking button in the UpdatePanel the table disappears.

nickbits: I did not know that about Page_Init. I am giving each control a unique ID and keeping track of them in arrays after they have been added to the tablecells though.

Yikes... this whole thing was working fine until this bug was discovered this morning. There's an Update button in another UpdatePanel that saves all the data (selected dropdown items, whether or not checkboxes have been checked) to the database and this add new row thing works if you hit Update after you select the dropdown item after each new row and before you add a new one. Even if I could do a save automatically right before creating a new row that might work. Argh.
 
Originally posted by: clamum
Jackdruid: I did have it in an !IsPostback check but after clicking button in the UpdatePanel the table disappears.
.

did you rebind data for the button_click event inside updatepanel?
 
Originally posted by: JACKDRUID
Originally posted by: clamum
Jackdruid: I did have it in an !IsPostback check but after clicking button in the UpdatePanel the table disappears.
.

did you rebind data for the button_click event inside updatepanel?
You mean call the BuildTable() method again inside the Button_Click? Yes. That method in turn binds the dropdown lists to their data source.
 
Can you add this to the Page_Init and see if it helps:

AJAXSciptManager.RegisterPostBackControl(button1);

Assumptions: AJAXScriptManager is the ID of your ScriptManager. button1 is the id of the first button (in your case, this presumably will be done in the for loop and each new button being created is registered for post back).

Also, this means you'll have to move BuildTable to Page_Init.

Edit: BTW, if this doesn't work, try and register all the other controls for post back (might take some trial and error).

Edit 2: I'm also assuming that you're persisting new data created on the client between post backs!? UpdatePanel does a regular post back, but the response is replaced by the server's response (which doesn't have any clue about things that might have happened on the client side).
 
I tried moving the BuildTable() method to the Page_Init() method, or part of it, but that did not work. Too many things depend on other things only created in Page_Load(). Also it didn't see another dropdown list that was databound earlier in Page_Init() in an !IsPostback section. I'd have to re-do the entire page as the example I described above is not nearly as complex as what is going on, just a piece of the whole and what I was interested in.

I ended up re-doing how the table is built and this solved the checkbox clearing problem but not dropdown (which I suspect is because of the .DataBind() call on every postback). I was originally doing a tblTable.Rows.Clear() call at the top of the method because I was running into exceptions but I removed the Clear() call and re-designed a bit how the table is generated. I also came across this link about Control Building and ViewState while doing a bit of Google searching, which was interesting.

So it's about half or 2/3rds fixed, and I was more concerned about the checkboxes clearing since there's up to 30 of them for a row, and I'm pretty sure I know how to fix the dropdown list clearing by persisting which dropdown has which value selected across postbacks. This should not be much data since there will not be very many rows.

I've only really seriously started working on ASP.NET applications within the last year and I've learned a ton, but there's always more to learn. I'd really like to be able to understand the ASP.NET page lifecycle and object model much more than I do. You obviously don't need to know it real well to be able to create apps but it sure helps in these kind of situations and in the design of pages.

Thanks for the help guys, I can always count on AT. 🙂
 
Back
Top