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.
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 have seen posts which recommend that I populate the list within a if(!IsPostback) block.
However, I only generate the dropdown lists in response to postbacks so I dont think that will work for me.
What I currently do is databind the dropdownlists in Page_Init. However, when I inspect the list after a new selection is made (and therefore the autopostback has been triggered) using "inspect element" I see that the first item in the list is always "selected."
Please let me know how to keep the selection selected through a postback.
If you bind the control in Page_Init, then you'll lose your selection. Don't do that.
Bind the control once, then ViewState will retain the control data. But when the selection changes, that will be sent to the server, and the SelectedIndexChanged event will fire.
More generally, any properties you set on a control before the Render phase will be saved in ViewState. On the next Post Back, the control will load the contents of ViewState, and therefore will restore itself to the state it was in before the PostBack.
The control will then take note of any POSTed values that were sent on the PostBack. This includes changes in the selection, textbox contents, etc. The control will fire the appropriate somethingChanged event to indicate that there has been a change from the prior state of ViewState.
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
I'm adding controls at run-time to the Page Form object.
Each control has an Id.
On postback, I want to be able to access these controls and their values.
Currently, when I do a postback, the form has no controls in the Form.Controls Collection.
How can I rectify this?
Is this only possible if you add the controls to the page every time the page loads?
Dynamically added controls need to be added with every page load or else they will be lost.
However the viewstate of these controls can be maintained as long as they always get added with the same ID.
I believe you have to add the controls dynamically in order to access them on postback. So if you add a textbox dynamically, your event handler can't retrieve its value unless you add it again.
EDIT: One workaround I used was to add a predetermined set of server controls to the page and then use JavaScript to hide/show those elements. You avoid postbacks and you avoid the unnecessary tom-foolery associated with retrieving values from dynamically added server controls. Of course, this limits you to a predefined number of controls.
This was a big pet peeve of mine with ASP.NET web forms and is a factor in my decision to explore ASP.NET MVC - no more viewstate/postback mess.