Dynamic Dropdownlist Question

OnInit Section

string state_id =((DropDownList) Page.FindControl("ddl_statedrop")).SelectedValue.ToString().Trim();
Response.Write("state_id: " + state_id + "<br><br>");


in PopulateStateDrop, i dynamically create the dropdownlist, bind it to a dataset, and assign selected text and selectedvalue properties...i'm also assigning the following:

StateList.AutoPostBack = true;
StateList.EnableViewState = true;
StateList.ID = "ddl_statedrop";

my problem is i need to get the .SelectedValue of the dropdownlist while i'm still in the Onit section...right after each postback, but the 2 lines above don't print the selected value on postback- it's empty ... however, if i place the exact 2 same lines of code in the Page_Load() Event:
string state_id =((DropDownList) Page.FindControl("ddl_statedrop")).SelectedValue.ToString().Trim();
Response.Write("state_id: " + state_id + "<br><br>");

then the currently selected value prints out fine every time....how can i access this value while still in the OnInit section?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try putting it in the databinding event instead of the init.
friskyweaselAuthor Commented:
hi Torrwin - sorry i'm not sure i follow...
to elaborate a bit...i have all kinds of additional controls that i'm dynamically creating in the OnInit() - this dropdownlist value i'm trying to get at the very start of OnInit will be used for calculations for every additional control that i create...so I kind of need to be able to actually have that value inside the OnInit() - can u explain in a little more detail exactly how to accomplish your suggestion?
DBAduck - Ben MillerPrincipal ConsultantCommented:
You cannot get the value of the control in OnInit because on Postback, the page needs to get to the event of pulling viewstate to populate the controls state and getting postback data into the controls that are created in OnInit.

In PageLoad it has gone through the process of getting the data matched back up to the controls and so you can get the data.

So I am not sure that in your case you will be able to get what you want in the OnInit.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I agree with dbaduck.  What is the reason you need it done in the Oninit section that can't be done in the page load?
friskyweaselAuthor Commented:
two very good responses - thanks to both of you - after doing some reading i was beginning to come to the same conclusion, but now after seeing your responses i feel more sure about it

torrwin - my initial reasoning for wanting to do it this way was because right after capturing the state drop value in the OnInit section, i then go on to create several textbox controls dynamically also (i had read somewhere here on EE that in order for the dynamically created textbox values to be retained properly across postbacks, they needed to be re-created on every page load - typically in the OnInit section - that's how all the OnInit business got started initially)

I was wanting to save a step by going ahead and assigning the textbox values at the time of their creation, but i needed that state dropdown value to do it - in the end it really wasn't that big of a deal though...as you both said all i have to do is go ahead and create my textbox controls, then in the Page_Load i call a seperate block of code to go through and populate their values, since at that point i will have my state drop val to work with

thanks to both for the enlightenment
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.