views:

37

answers:

2

Hello to all.

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.

A: 

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.

Ben Robinson
Ben's solution worked for me. Thank you to all who responded to my question, it's very much appreciated.
frank
A: 

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.

Yves M.