I create some tables dynamically in C#, and they are giving me viewstate issues. I have a checkbox table and a number entry table that depends on the first. When I press a button that causes postback, I get some problems. Here is a flow chart.
Page_Load
create checkbox table
create number entry table (check box is always empty, table has wrong number of rows, but numbers are populated correctly from postback)
Postback button
create number entry table (check box is now populated, table has correct number of rows, but numbers did not survive postback).
Is there a way I can load the viewstate to the checkbox table before I create the number entry table? I have tried to create the number entry tables in one or the other place (on page load or postback button method), but neither is completely correct. I know that dynamic content needs to be created on every load for the viewstate to appear.
I suspect I may need to create a datatable and store that in the viewstate for the checkboxes to reappear correctly...
I would add your dynamic controls in the PreInit or Init event of your page. Although you can add them afterwards, after the Init phase is when ViewState data is loaded for controls.
If you add your controls during the Page_Load the control has to play catch up, which means it runs through all of the events in the page lifecycle until it catches up with the rest of the page. This might be causing the issue you're seeing.
Here's an excellent write-up regarding the Page Lifecycle and ViewState:
http://www.codeproject.com/Articles/24611/ASP-NET-Internals-Viewstate-and-Page-Life-Cycle
Related
I have a Gridview with three or more pages' worth of data. The user will check checkboxes in all these pages and click the submit button. The checked values in all the pages need to be stored in the database.
The problem is only the selected page's values got stored to the database and not the checked values on other pages.
I'm assuming you're using Web Forms. I think pagination in a GridView causes a post back event, which means any state that isn't in the server side is lost.
You need to store which entries are checked in server state somehow (putting a Dictionary or List in the ViewState for example) either each time you check a box, or each time you page. Then, when you page, you should use the saved state to restore the check box values on the current page.
First off, I have managed to create a web application where my dynamically created user controls are recreated and repopulated with the correct information upon postback. I am not sure what my problem is, but i hope that you will be able to help me figure it out based on my situation:
On my page i enter the number of controls to be created into a hardcoded textbox (its on the aspx page) and click the okay butten. This in turn, creates the specified number of user controls dynamically using c# in the background.
So far the desired number of dynamic controls are in a table on the page.
Next...
I have 1 textbox and 4 dropboxes on each dynamic user control. When i type a company name into the textbox field and press enter or click away (on text changed event) it autoposts back and the textbox retains the company name that i have typed in.
Based on this string the dropboxes are populated from the database. Now when i select the desired items from the dropboxes and click on the save button (located outside of the dynamic controls, on the page) it does an insert to the database, but it turns out that upon this postback the indexes from the dropboxes have been reset and the wrong values get inserted.
The following pictures show firstly, how it should be and then how it is.
Basically the company name remains in the textbox of the dynamic control, but the information i choose from the dropbox resets to the first index.
It's hard to tell what happend without code, but this is a common mistake:
If you fill/create the dropdownlist controls in the page load event and you post back, the code will refill/recreate the controls. That's why you have to use something like If(!IsPostBack) in your page load event. Otherwise it will execute that code everytime you do a postback and actually just want to execute the code in your event handler for that button.
If you're dynamically creating the controls, make sure to do that in the Page_Init event. Dynamic controls have to be recreated on every postback. Their state is restored after the Page_Init (if it is a postback), so make sure to only set their values in Page_Load if you want to overwrite them.
I'm working on an asp.net website (using C#). I have implemented a detailsview as part of a data entry system for this website.
The detailsview contains a drop down list used to associate a category with the record being submitted to this data entry system.
The code behind file accesses a datasource (an SQL server 2005 database table), to determine the fields associated with a selected category and to generate checkbox controls based upon the fields available in that category
I understand (I think) the .net page lifecycle, and the necessity to add dynamic controls on each postback to maintain the controls and their "state". However:
I've read that I must add dynamic controls in the Page_Init/initialisation phase of the page lifecycle, in order for the dynamic controls properties and events to be available upon a postback
The value I require to query the datasource (and to determine the number and names of the dynamic controls for a category selection) is assigned in the dropdown list's SelectedIndexChanged event handler, which is always processed after the Page_init event
I'm not sure how I can pass the required value (the dropdown list's selected index) to the Page_Init event at the correct point in the page lifecycle (the Page_init event).
I would greatly appreciate any pointers/assistance from the stackoverflow community
and thank you for taking the time to read this post.
You do not have to add the controls in the init, you can add them in page_load just fine as well. It is often recomended to add them in the init as this is the point in the page lifecycle that controls defined in the markup are instantiated. Why do you need to assign the value to determine whether the controls should be added in the SelectedIndexChanged event. If it is based on the SelectedValue of a drop down list, can you not simply access the SelectedValue and assign the value on each post back, even if it has not changed. Then you could do it in the Page_Load and then add your controls afterwards.
The value you are after is posted back to the server and can be found in Request.Form NameValueCollection. The key is the name of the dropdown list.
My problem is:
I've got a table, dynamically created, fill with a lot of dropdownlists witches IDs are dynamically created.
When a button is pressed, I need to scan all controls in the table and save their value.
But after the postback I can't no longer access to the table, and I've no idea how can I get those values...
Thanks!
Controls created dynamically must be created again on every postback on the event Init or PreInit (before ViewState is loaded) otherwise you won't be able to retrieve their values.
Some reference links
http://msdn.microsoft.com/en-us/library/ms972976.aspx#viewstate_topic4
https://web.archive.org/web/20210707024005/http://aspnet.4guysfromrolla.com/articles/081402-1.aspx
https://web.archive.org/web/20210707024009/http://aspnet.4guysfromrolla.com/articles/082102-1.aspx
If the form was posted, shouldn't they be in the Request.Forms collection.
Let's say you named them all starting with dct.
Then you could loop through the collection and taking what values you need.
You could access the values with Request.Form("dct_001") etc...
Since the lookup is string based you could put it in a loop to catch the value.
BTW this classic ASP approach still works in 4.0
using c# (asp.net)
i'm programmatically creating several drop down lists (random number).
the user selects a particular value from each list and i need to save the user selected value.
if i try to save the data to session state by using a button click event it says the drop down list object hasn't been created. (obviously cuz i'm creating the drop down lists in the page load event under !IsPostBack.)
if i try to save the data to session state in the page load event under IsPostBack i only get the first value from each list. (obviously cuz when the page is recreated after the postback, the drop down lists have been recreated and the user entered data is lost).
How do i save the user selected value from the drop down lists when a button is clicked?
i'm doing this in the code behind file.
As John Saunders said, you have to recreate all of your DropDownList controls on every postback. Additionally, you have to create them before state is restored. That means that "under !IsPostBack" in the page load event is already too late. Move it up to Page Init.
You have to recreate all of the dropdowns on every postback.
Personally (without the ability to switch to MVC) I would consider getting rid of the postbacks altogether and reading the values back directly from a standard HTTP POST request.
The implementation of dynamic controls with postbacks often ends up rather convoluted and difficult to follow