首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

DropDownList默认显示数据库中的值,而不是第一个值解决方法

2012-03-08 
DropDownList默认显示数据库中的值,而不是第一个值我是个AJAX的三级联动 的省,市、县下拉框。现在我想让它默

DropDownList默认显示数据库中的值,而不是第一个值
我是个AJAX的三级联动 的省,市、县下拉框。现在我想让它默认显示的是从数据库中读取到的值,而不是默认的第一值。
 public void bindclass()
  {
  this.DropDownList1.DataSource = AjaxMethod.GetProvinceList();

  this.DropDownList1.DataTextField = "PROVINCESNAME";
  this.DropDownList1.DataValueField = "CODE";
  this.DropDownList1.DataBind();
  this.DropDownList1.Items.Insert(0, new ListItem("请选择省", "0"));
  model = bll.GetModel(int.Parse(Request.Params["id"]));
  this.DropDownList1.SelectedValue = model.o_Province;//默认显示数据库中的值

  this.DropDownList1.Attributes.Add("onchange", "cityResult();");
  this.DropDownList2.Attributes.Add("onchange", "areaResult();");

  this.Introdry_1.DataSource = AjaxMethod.GetIntrod_1();
  Introdry_1.DataTextField = "GB_SortName";
  Introdry_1.DataValueField = "Id";
  Introdry_1.DataBind();
  this.Introdry_1.Items.Insert(0, new ListItem("请选择行业", "0"));
  Introdry_1.Attributes.Add("onclick", "Introd_2();");

  }
这是我的代码, this.DropDownList1.SelectedValue = model.o_Province;//默认显示数据库中的值

这样写的不起作用、

[解决办法]
设置DropDownList1.SelectValue等于你想显示那个对象的ID值 , 尼玛这么简单的问题你也问 - -。
[解决办法]
this.DropDownList1.DataBind(); 放到 SelectedValue 代码之后
[解决办法]

探讨
this.DropDownList1.DataBind(); 放到 SelectedValue 代码之后

[解决办法]
探讨

引用:
this.DropDownList1.DataBind(); 放到 SelectedValue 代码之后


顶!

[解决办法]
通过在项里找从数据库中得到的某一个值,然后设置被选中即可。

this.DropDownList1.Items.FindByText(model.o_Province).Selected = true;//默认显示数据库中的值

this.DropDownList1.Items.FindByValue(model.o_Province).Selected = true;//默认显示数据库中的值

热点排行