如何设置下拉列表控件的默认值? [英] how to set the default value to the drop down list control?

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

问题描述

我的网页上有一个下拉列表控件.我已将数据表绑定到下拉列表控件,如下所示 -

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.

如何做到这一点?

推荐答案

After your DataBind():

After your 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;

这篇关于如何设置下拉列表控件的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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