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
Related
Here is how my code is set up.
The webpage itself works like this:
You have a drop down that allows you to select between different values. Lets call it dropdown A. Depending on the value selected, a gridview gets generated.
How generation works:
When a item in the dropdown A gets selected, inside the selectedIndexChanged is a method call to a function that creates a DataTable. That datatable gets binded to the gridview inside selectIndexChanged.
When it gets bounded, onRowBoundEvent gets called, and this is where I add all the necessary controls with unique IDs.
There is a button called saved that looks at the data in gridview, and saves it.
Problem: When I press save, there are no controls in the gridview for me to find.
I can use findControl since I know all the ids, but how do I make the controls stick around?
If I bind it in the page_load, how do I know what gridview to generate since if I select a value from dropdown A, page_load still fires before I can get a selection value from dropdown A, so I can't make a simple conditional statement based on the dropdown value.
I can't show any code, sorry. But this is more of a conceptual question I have.
I was able to figure this out on my own.
PrePage_Load during the project lifecycle has access to controls. Dropdown A in my example can be accessed in PrePage_Load, allowing me to get the necessary values and set them before Page_Load starts.
For controls in the gridview, I made it so the gridview does not automatically load from viewstate, and I rebuilt the gridview myself during page_load.
I am trying to dynamically generate checkbox controls in web part (SP 2010) but the number of checkboxes that has to be created depends on the value from previous web part. Abiding by the web part communication infrastructure, I can get that value only on "onprerender" stage. But if I try to create checkboxes with that value count, I cannot get the selected values of the checkbox on postback! I am aware that dynamically created controls should be placed either in page_load or oninit. But, here am supposed not to put there since am getting the particular value in prerender stage. Please help! Am struck with this!
Asp.net needs you to recreate dynamically added controls on every Init after they are first added, till you require those controls on page.
You can use a Session variable to keep track of your count.
If a control is not present when the ProcessPostData method is invoked(Called just after LoadViewState), you cannot receive user input for them. Events like CheckedChanged also will fire only if your controls are present in the Controls collection before ProcessPostData is called.
Refer: Page life cycle
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
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.
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