Dynamic Dropdownlist Question

Posted on 2006-04-10
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 can i access this value while still in the OnInit section?
Question by:friskyweasel
    LVL 13

    Expert Comment

    Try putting it in the databinding event instead of the init.

    Author Comment

    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 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 24

    Accepted Solution

    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

    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

    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 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

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now