我可以用来填充 DropDownList 的最舒服的数据类型是什么?
例如,我希望用户为给定团队创建一个新的粉丝俱乐部。
显然,团队必须加载到 DropDownList 中,以便从预设的选择组中进行选择。团队列表是从数据库返回的。
截至目前,我正在做这样的事情:
//FindAll() returns IQueryable<Team>
var Teams = teamsRepo.FindAll().AsEnumarable();
myDropDownList.DataTextField = "Name";
myDropDownList.DataValueField = "ID";
myDropDownList.DataSource = Teams;
myDropDownList.DataBind();
不幸的是,当以这种方式做事时,我没有强类型属性,因此存在拼写错误 ValueField 或 TextField 的风险。
尝试获取 DropDownList 的选定值时也存在问题。使用以下代码,无论选择哪个团队,所有内容都会以 ID 1 保存。
fansite.IDTeam = myDropDownList.SelectedIndex;
关于如何改进有什么建议吗?
For example, I want a user to create a new fanclub for a given Team.
Obviously the teams have to be loaded into a DropDownList so they are chosen from a pre-set choice group. The list of teams are returned from a database.
As of now I'm doing things like this:
//FindAll() returns IQueryable<Team>
var Teams = teamsRepo.FindAll().AsEnumarable();
myDropDownList.DataTextField = "Name";
myDropDownList.DataValueField = "ID";
myDropDownList.DataSource = Teams;
myDropDownList.DataBind();
Unfortunately when doing things this way I have no strongly typed attributes, so there is a risk of misspelling the ValueField or TextField.
There is also a problem when trying to get the selected value of the DropDownList. Using the following code, everything is saved with ID 1 regardless of what team was chosen.
fansite.IDTeam = myDropDownList.SelectedIndex;
Any suggestions on how to improve?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从您的问题来看,您似乎担心代码中数据绑定的硬编码字符串。同样的事情也曾经让我发疯。
有一种使用 lambda 方法和表达式树的好方法,它允许您摆脱硬编码字符串并以强类型方式获取属性名称:
您可以使用此类和扩展方法...
...并且这将允许您执行以下操作:
这就是我喜欢看到的;看不到硬编码的字符串文字! :-)
From your question it seems you're concerned about the hard coded strings for the databinding in your code. The same thing used to drive me nuts too.
There is a good way of using lambda methods and expression trees that will allow you to get rid of the hard coded strings and get the property names in a strongly typed way:
You can use this class and extension method...
...and this will allow you to do the following:
That's what I like to see; not a hard coded string literal in sight! :-)
这正是 .NET 数据绑定的工作原理,您必须输入属性名称。
您始终可以将转换器添加到组合框项,然后调用 add (ms-doc)
至于获取所选项目的值,请尝试以下操作:
This is exactly how .NET databinding works, you have to type in the property names.
You could always add a converter to a combobox item and then call add (ms-doc)
As far as getting the value of the selected item try this:
(这是在ASP.Net澄清之前写的,我是从winforms的角度回答的)。
我不确定您的
Teams
ID 是什么样的,但如果它们不是连续的、从零开始且没有间隙的,则使用myDropDownList.SelectedIndex;
会给您错误的结果一条数据。我认为您最后一行应该阅读:
在要使用的数据类型上,我通常使用
Dictionary
作为下拉列表,并有一些辅助扩展方法来使填充更好一点。(This was written before the ASP.Net clarification, I have answered from a winforms persepective).
I'm not sure what your
Teams
IDs are like, but if they are not sequential, zero based and with no gaps, usingmyDropDownList.SelectedIndex;
will give you the wrong piece of data.I think you last line should read:
On the data type to use, I usually use a
Dictionary<int, string>
for dropdown lists, and have a few helper extension methods to make population a little nicer.您应该考虑使用
ObjectDataSource
或LinqDataSource
等数据源来数据绑定您的控件。它将节省您的时间并且不易出错,因为您将通过向导配置设置,而不是在代码隐藏中定义它们。
You should consider using DataSources like
ObjectDataSource
orLinqDataSource
to DataBind your controls.It will save you time and is less error prone because you will configure your settings through a wizard instead of defining them in your code-behind.