在 Page PreRender 处使用 c# 获取对下拉框的引用
我想在加载期间从后面的 C# 代码向我的 aspx 页面上的下拉框添加选项。 我不知道如何获得对控件的引用。 我有一些 C# 代码,当用户更改下拉框时会触发这些代码。 我得到了对下拉菜单的引用:
DropDownBox ddb = (DropDownBox)info.Toolbar.ToolbarItems.Find("ID");
尝试它,那将不起作用
protected void Page_PreRender(object sender, EventArgs e)
{
但是如果我在我的 aspx.cs 上
我缺少什么? 谢谢。
I want to add options to a dropdownbox on my aspx page from the c# code behind during load. 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. 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
What am I missing? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望在回发时从视图状态中自动选择下拉列表中的选定项目,则需要在 Page_PreLoad 触发时拥有下拉列表中的所有项目。 为此,您需要将代码放入 Page_Init 中,此时控件已创建,但视图状态尚未注入其中。
请查看此处 ASP.NET 页面生命周期概述,了解有关以下内容的信息页面生命周期。
我看到您的下拉列表位于父容器中,如果无法获取对控件的引用,您可能需要在使用 Find() 之前调用 info.EnsureChildControls() 。
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. 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.
Take a look here ASP.NET Page Life Cycle Overview for info on the page lifecycle.
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.
PreRender 即将结束页面生命周期。 您确定要在那里进行更改吗? 听起来您应该在最初绑定下拉列表或更改其选择时更改下拉列表中的项目。
http://codebetter.com/blogs//images/ codebetter_com/raymond.lewallen/89/o_aspNet_Page_LifeCycle.jpg
PreRender is toward the end of the page lifecycle. 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