日期:2014-05-18  浏览次数:20475 次

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;//默认显示数据库中的值