为什么这个 DropDownList 数据绑定到 List?不工作?
我正在尝试将 List
绑定到用户控件中的 DropDownList
。我认为我正在做正确的事情,但似乎在我的代码执行后绑定被清除。这是供审查的代码!
用户控制:
<asp:DropDownList ID="subjectNameDropDown" runat="server"/>
<asp:DropDownList ID="yearLevelDropDown" runat="server"/>
自动生成设计的代码隐藏:
public partial class NewSiteMetadataUserControl {
protected global::System.Web.UI.WebControls.DropDownList subjectNameDropDown;
protected global::System.Web.UI.WebControls.DropDownList yearLevelDropDown;
}
代码隐藏:
public partial class NewSiteMetadataUserControl : UserControl
{
protected override void CreateChildControls()
{
subjectNameDropDown = new DropDownList();
yearLevelDropDown = new DropDownList();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
EnsureChildControls();
// Attempt 1
List<String> subjectNames = GetSubjectValues();
foreach (var subjectName in subjectNames)
subjectNameDropDown.Items.Add(subjectName);
subjectNameDropDown.DataBind();
// Attempt 2
List<String> yearLevels = GetYearLevelValues();
yearLevelDropDown.DataSource = yearLevels;
yearLevelDropDown.DataBind();
}
}
这种方法应该有效吗?
如果有效应该,如何调试代码执行后发生的情况?
I'm trying to bind a List<String>
to a DropDownList
in a user control. I think I'm doing the right thing, but it seems that after my code executes the bindings are cleared. Here's the code for review!
User control:
<asp:DropDownList ID="subjectNameDropDown" runat="server"/>
<asp:DropDownList ID="yearLevelDropDown" runat="server"/>
Auto-generated designed code-behind:
public partial class NewSiteMetadataUserControl {
protected global::System.Web.UI.WebControls.DropDownList subjectNameDropDown;
protected global::System.Web.UI.WebControls.DropDownList yearLevelDropDown;
}
Code-behind:
public partial class NewSiteMetadataUserControl : UserControl
{
protected override void CreateChildControls()
{
subjectNameDropDown = new DropDownList();
yearLevelDropDown = new DropDownList();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
EnsureChildControls();
// Attempt 1
List<String> subjectNames = GetSubjectValues();
foreach (var subjectName in subjectNames)
subjectNameDropDown.Items.Add(subjectName);
subjectNameDropDown.DataBind();
// Attempt 2
List<String> yearLevels = GetYearLevelValues();
yearLevelDropDown.DataSource = yearLevels;
yearLevelDropDown.DataBind();
}
}
Should this approach work?
If it should, how can I debug what happens after the code executes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这种方法应该可行,这就是为什么目前不行,
DataBind
完成的 DropDownList 需要DataSource
。这就是为什么尝试 #1 不起作用。List
,则没有明确的键/值对可供绑定。这就是为什么当绑定到List
(例如)时,您需要重写Person
类中的.ToString()
来提供键/值绑定,或手动设置DataTextField
、DataValueField
。字符串
的键/值对。考虑一下您想要什么 HTML。简单字符串的键/值应该是什么?这样做没有意义。
由于您并不真正关心“键”(只关心显示的内容),因此我建议您改为绑定到
Dictionary
。要么让你的方法返回该值,要么迭代列表并将它们添加到带有索引的字典中。
Yes, this approach should work, here's why it currently isn't,
DataBind
needs aDataSource
. This is why Attempt #1 is not working.List<string>
, there is no clear key/value pair to bind to. This is why when binding to aList<Person>
(for example), you need to override.ToString()
in thePerson
class to provide the key/value binding, or manually set theDataTextField
,DataValueField
.string
.Think about what HTML you want. What should be the key/value for a simple string? Doesn't make sense does it.
Since you don't really care about the "key" (only what is displayed), i suggest you bind to a
Dictionary<TKey,TValue>
instead.Either make your method return that, or iterate through the list and add them to the dictionary with an index.
这里的问题是
CreateChildControls
。在我尝试完成这项工作的过程中,我添加了这个初始化控件的方法。这是不必要的,实际上会导致数据绑定被清除,因为它是在OnLoad
之后由框架自动调用的。解决方案是删除此方法以及对
EnsureChildControls
的调用。The problem here was
CreateChildControls
. Somewhere in my attempts to make this work I added this method that initialises the controls. This isn't necessary and in fact caused the data bindings to be wiped out, as it was automatically called by the framework afterOnLoad
.The solution was to remove this method and the call to
EnsureChildControls
.