如何设置默认值下拉列表控件?
问题描述:
我有一个下拉我的网页列表控件。我已经将DataTable绑定到DropDownList控件如下: -
I have a drop down list control on my web page. I have bind the datatable to the dropdownlist control as follows -
lstDepartment.DataTextField = "DepartmentName";
lstDepartment.DataValueField = "DepartmentID";
lstDepartment.DataSource = dtDept;
lstDepartment.DataBind();
在页面加载事件我想设置的默认值下拉从我的其他表字段列表的控制。
in the page load event i want to set the default value to the drop down list control from my other table field.
如何做到这一点?
答
在你的的DataBind()
:
lstDepartment.SelectedIndex = 0; //first item
or
lstDepartment.SelectedValue = "Yourvalue"
or
//add error checking, just an example, FindByValue may return null
lstDepartment.Items.FindByValue("Yourvalue").Selected = true;
or
//add error checking, just an example, FindByText may return null
lstDepartment.Items.FindByText("Yourvalue").Selected = true;