鉴于以下内容
public class MyControl : CompositeControl { private DropDownList myList; protected override void CreateChildControls() { base.CreateChildControls(); myList = new DropDownList(); myList.AutoPostBack = true; this.Controls.Add(myList); if (!Page.IsPostBack) { myList.DataSource = MyBLL.SomeCollectionOfItems; myList.DataBind(); } } }我发现列表中的项目保持正确,但是当呈现不同的控件,然后再次呈现这个控件时,最后选择的项目不会被保留。 (列表中的第一项始终被选中)
如果最后选定的项目自动保存在ViewState中,还是我期望太多?
Given the following
public class MyControl : CompositeControl { private DropDownList myList; protected override void CreateChildControls() { base.CreateChildControls(); myList = new DropDownList(); myList.AutoPostBack = true; this.Controls.Add(myList); if (!Page.IsPostBack) { myList.DataSource = MyBLL.SomeCollectionOfItems; myList.DataBind(); } } }I find that the items in the list persist properly, but when a different control is rendered and then this one is rendered again, the last selected item is not persisted. (The first item in the list is always selected instead)
Should the last selected item be persisted in ViewState automatically, or am I expecting too much?
最满意答案
我认为这是一个隐藏的ViewState问题。 您可以在CreateChildControls中创建并绑定一个控件。 你应该只在这个地方创建控件。 将绑定代码移动到类加载事件并使用EnsureChildControls。
I think this is a hidden ViewState issue. You create and bind a control in CreateChildControls. You should only create the control at this place. Move the binding code to the classes load event and use EnsureChildControls.
更多推荐
myList,项目,ViewState,DropDownList,最后,电脑培训,计算机培训,IT培训"/> <meta name
发布评论