自定义 WebControls.DropDownList ViewState
我正在尝试创建一个自定义 Web 控件,它只是一个简单的 DropDownList,它预加载了美国的所有州。它在大部分情况下都可以工作,但是在回发后控件将恢复到其初始状态。 (我可以在回发中获取所选值)。为了使列表在回发后保留其所选项目,我需要实施什么?
这是我的控件类:
[DefaultProperty("Text")]
[ToolboxData("<{0}:StateDropDownList runat=server></{0}:StateDropDownList>")]
public class StateDropDownList : DropDownList
{
private void Populatedropdown()
{
this.Items.Clear();
this.Items.Add(new ListItem("Alabama", "AL"));
/* the rest .... */
this.Items.Add(new ListItem("Wyoming", "WY"));
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
Populatedropdown();
}
}
编辑
这不是从数据源反弹控件的问题。此自定义控件的目标是避免不断绑定状态表中的下拉列表的需要。我试图解决的问题是如何在回发时维护此自定义下拉列表的选定值?
I'm trying to create a custom web control that's just a simple DropDownList which is preloaded with all the states in the US. It's working for the most part, however after post back the control reverts to its initial state. (I am able to get the selected value in the post back). What will I need to implement in order for the list to retain it's selected item after post back?
Here's my control class:
[DefaultProperty("Text")]
[ToolboxData("<{0}:StateDropDownList runat=server></{0}:StateDropDownList>")]
public class StateDropDownList : DropDownList
{
private void Populatedropdown()
{
this.Items.Clear();
this.Items.Add(new ListItem("Alabama", "AL"));
/* the rest .... */
this.Items.Add(new ListItem("Wyoming", "WY"));
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
Populatedropdown();
}
}
EDIT
This is not an issue with the control being rebound from a datasource. The goal of this custom control is to avoid the need to constantly bind drop down lists from a state table. The problem I'm trying to resolve is how do I maintain the selected value of this custom dropdown on postback?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将
ListItems
添加到 控制生命周期。将您的
Populatedropdown
调用移至 OnInit 方法中,您的问题就会消失。You are adding the
ListItems
to late in the control life cycle.Move your
Populatedropdown
call into the OnInit method and your problem should go away.你在做这个吗?
Are you doing this?