将列表绑定到下拉列表,用值做什么? [英] bind list to dropdownlist, what to do with value?

查看:113
本文介绍了将列表绑定到下拉列表,用值做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一个列表绑定到一个DropDownList.但我想为每个列表项的值赋予其他值.

I am binding a List to a DropDownList. But I want to give other values to the value per list item.

我有以下ddl和清单:

I have following ddl and list:

List<string>

体育,排球,橄榄球

<select>
    <option selected="selected" value="0"> Alle soorten</option>
    <option value="sport">sport</option>
    <option value="volleyball">volleyball</option>
    <option value="rugby">rugby</option>
</select>

但是我想关注(值中的ID)

But I want following (ID in the value)

<select>
    <option selected="selected" value="0"> Alle soorten</option>
    <option value="1">sport</option>
    <option value="2">volleyball</option>
    <option value="3">rugby</option>
</select>

如何创建列表以获取上述下拉列表.

How do I need to create a List so I can get the above dropdownlist.

谢谢

推荐答案

如果您能够更改源类型,我建议您使用字典.您可以这样操作:

If you are able to change the type of your source I would recommend to use a dictionary. You can do it this way:

var source = new Dictionary<int, string>();
source.Add(0, "Sports");
source.Add(1, "Football");
dropDown.DataSource = source;
dropDown.DataTextField = "Key";
dropDown.DataValueField = "Value";
dropDown.DataBind();

这将导致以下结果:

<select name="DdlCat2" id="DdlCat2" class="cats">
    <option selected="selected" value="Sports">0</option>
    <option value="Football">1</option>
</select>

以后您可以像下面那样访问ID或值:

Later you can access the ID or value like this:

dropDown.SelectedItem.Value
dropDown.SelectedItem.Text

这篇关于将列表绑定到下拉列表,用值做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆