以编程方式设置下拉列表 [英] Setting dropdownlist selecteditem programmatically

查看:118
本文介绍了以编程方式设置下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式设置ASP.Net下拉列表控件的 selecteditem 属性。



所以我想将值传递给下拉列表控件以设置项目等于传递值的所选项目。

解决方案

假设列表已经是数据绑定,您可以在下拉列表中简单地设置 SelectedValue 属性。

  list.DataSource = GetListItems(); //<  - 从某个地方获取您的数据。 
list.DataValueField =ValueProperty;
list.DataTextField =TextProperty;
list.DataBind();

list.SelectedValue = myValue.ToString();

myValue 变量的值将需要存在于控件数据绑定中 DataValueField 中指定的属性中。



更新
如果 myValue 的值不存在作为具有下拉列表选项的值,它将默认在下拉列表中选择第一个选项。 p>

I want to set the selecteditem attribute for an ASP.Net dropdownlist control programmatically.

So I want to pass a value to the dropdownlist control to set the selected item where the item is equal to the passed value.

解决方案

Assuming the list is already data bound you can simply set the SelectedValue property on your dropdown list.

list.DataSource = GetListItems(); // <-- Get your data from somewhere.
list.DataValueField = "ValueProperty";
list.DataTextField = "TextProperty";
list.DataBind();

list.SelectedValue = myValue.ToString();

The value of the myValue variable would need to exist in the property specified within the DataValueField in your controls databinding.

UPDATE: If the value of myValue doesn't exist as a value with the dropdown list options it will default to select the first option in the dropdown list.

这篇关于以编程方式设置下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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