简体   繁体   English

在Page PreRender上使用c#获取对下拉框的引用

[英]Get reference to a dropdownbox with c# at Page PreRender

I want to add options to a dropdownbox on my aspx page from the c# code behind during load. 我想在加载过程中从背后的C#代码向我的aspx页面上的下拉框添加选项。 I don't know how to get a reference to the control. 我不知道如何获得对该控件的引用。 I have some c# code that is triggered when the user changes the dropdownbox. 我有一些用户更改下拉框时触发的C#代码。 In that I get a reference to the dropdown with: 在那我得到了对下拉菜单的引用:

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

But that won't work if I try it in 但是如果我尝试的话那是行不通的

protected void Page_PreRender(object sender, EventArgs e)
    {

on my aspx.cs 在我的aspx.cs上

What am I missing? 我想念什么? Thanks. 谢谢。

If you want the selected item of the dropdown to be automatically selected from viewstate on postbacks you will need to have all the items in the dropdown by the time Page_PreLoad fires. 如果您希望从回发时的viewstate中自动选择下拉菜单中的所选项目,则需要在触发Page_PreLoad时在下拉菜单中具有所有项目。 To do this you will want to put your code in Page_Init, at this point the controls are created but viewstate has not yet been injected into them. 为此,您需要将代码放入Page_Init中,此时已创建控件,但尚未将viewstate注入控件中。

Take a look here ASP.NET Page Life Cycle Overview for info on the page lifecycle. 在此处查看ASP.NET页面生命周期概述,以获取有关页面生命周期的信息。

I see that your dropdown is in a parent container, you may need to call info.EnsureChildControls() before you use Find() if it is not able to get a reference to your control. 我看到您的下拉列表位于父容器中,如果无法找到对控件的引用,则可能需要在使用Find()之前调用info.EnsureChildControls()。

PreRender is toward the end of the page lifecycle. PreRender即将到页面生命周期的尽头。 Are you sure you want to be making changes there? 您确定要在此进行更改吗? Sounds like you should be changing items in the dropdown when it is initially bound or when its selection is changed. 听起来好像您应该在初始绑定或更改其选择时更改下拉菜单中的项目。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM