本文介绍了C#下拉列表绑定默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当dropdownlist具有从数据库数据集中获取的datatext字段和datavalue字段时,如何在零索引位置添加默认值选择一个".
how can i add default value "select one" at zero index position when dropdownlist is having datatext field and datavalue field which is obtained from database througt dataset.
推荐答案
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// you must set the connection first.
// e.g c.setcon(); // here this is method for setting a connection
DataSet ds = new DataSet();
SqlDataAdapter myda = new SqlDataAdapter("Select field1 FROM Table", connection object);
myda.Fill(ds);
DropDown1.DataSource = ds;
DropDown1.DataValueField = "field1";
DropDown1.DataBind();
DropDown1.Items.Insert(0, new ListItem("Select One", "0"));
}
}
希望这能对您有所帮助,否则请贴出来.
hope this will help you, if not then dear please post it.
select 0, "select one"
UNION
select colA, colB from tableA
这篇关于C#下拉列表绑定默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!