[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Dynamic Dropdownlist Question

Posted on 2006-04-10
Medium Priority
Last Modified: 2012-05-05
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?
Question by:friskyweasel
  • 2
  • 2
LVL 13

Expert Comment

ID: 16421573
Try putting it in the databinding event instead of the init.

Author Comment

ID: 16421741
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?
LVL 25

Accepted Solution

DBAduck - Ben Miller earned 1600 total points
ID: 16424169
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.

LVL 13

Assisted Solution

Torrwin earned 400 total points
ID: 16426385
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?

Author Comment

ID: 16426811
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Integration Management Part 2
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month20 days, 7 hours left to enroll

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question