设置DropDownList的编程将selectedItem [英] Setting dropdownlist selecteditem programmatically

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

问题描述

我想设置selectedItem设置为ASP。净DropDownList控件编程。

I want to set selecteditem for asp. net dropdownlist control programmatically.

所以我想一个值传递给DropDownList控件设置选择的项目在哪里项等于传递的值。

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

推荐答案

假设名单已经是数据绑定你可以简单地将的SelectedValue 属性的下拉列表中。

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();

myvalue的变量的值需要在你的控件绑定在 DataValueField 指定的属性存在

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

更新
如果 myvalue的不存在与下拉列表中选择一个值,该值将默认为选择下拉列表中的第一个选项。

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.

这篇关于设置DropDownList的编程将selectedItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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