asp.net的dropdownlist服务器空间数据绑定

2011-1-5 12:57:14 - tanlixiao

1. 绑定datatable
DataTable dtCategory=new DataTable();
dropdownlist.DataValueField = "ID";
dropdownlist.DataTextField = "categoryName";
dropdownlist.DataSource = dtCategory;
dropdownlist.DataBind();

2.插入一行
dropdownlist.Items.Insert(0, new ListItem("请选择", "-1"));

3.添加一行
dropdownlist.Items.Add(new ListItem(“text”,"value"));

下面是判断下拉控件是服务器控件还是表单页面控件,然后绑定数据
if (ddlDistrict is DropDownList)
{
  var control = ddlDistrict as DropDownList;
  control.DataSource = dt;
  control.DataTextField = "DistrictName";
  control.DataValueField = "DistrictID";
  control.DataBind();
  control.Items.Insert(0, defaultItem);
}
else if (ddlDistrict is HtmlSelect)
{
  var control = ddlDistrict as HtmlSelect;
  control.DataSource = dt;
  control.DataTextField = "DistrictName";
  control.DataValueField = "DistrictID";
  control.DataBind();
  control.Items.Insert(0, defaultItem);
 }

标签:dropdownlist

关于我

喜欢简简单单的生活.

记录平凡世界的点滴

-于2010年12月31日

联系我

  • 微信:fishren
  • 邮箱:
    tanlixiao@163.com
  • Address:
    中国最南端的滨海城市
    河西区 解放路 502