CompositeControl中的DropDownList应该记住选定的项目吗?(Should a DropDownList within a CompositeControl remember s

编程入门 行业动态 更新时间:2024-10-24 02:25:51
CompositeControl中的DropDownList应该记住选定的项目吗?(Should a DropDownList within a CompositeControl remember selected item?)

鉴于以下内容

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

本文发布于:2023-04-28 01:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329808.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:项目   DropDownList   CompositeControl   item   selected

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!