Get dropdown link from C # in PreRender page

I want to add options to the dropdownbox in my aspx page from the C # code that is at load time. I don't know how to get the link to the control. I have C # code that runs when the user changes the dropdown. In this I get a link to the dropdown:

DropDownBox ddb = (DropDownBox)info.Toolbar.ToolbarItems.Find("ID");

      

But it doesn't work if I try it in

protected void Page_PreRender(object sender, EventArgs e)
    {

      

on my aspx.cs

What am I missing? Thank.

0


source to share


2 answers


If you want the selected dropdown item to be automatically selected from viewstate in postbacks, you will need to have all items in the dropdown by the time Page_PreLoad starts. To do this, you will want to put your code in Page_Init, at this point the controls will be created but the viewstate has not yet been injected into them.

Take a look here at ASP.NET Page Life Cycle Overview for page life cycle information.



I see the dropdown is in the parent container, you may need to call info.EnsureChildControls () before you use Find () if it fails to get a reference to your control.

+1


source


PreRender is coming to the end of the page's lifecycle. Are you sure you want to make changes there? It looks like you should be changing the items in the dropdown when it is initially linked or when its selection changes.



http://codebetter.com/blogs//images/codebetter_com/raymond.lewallen/89/o_aspNet_Page_LifeCycle.jpg

0


source







All Articles