读书人

DropDownList默许显示数据库中的值而

发布时间: 2013-03-12 11:19:35 作者: rapoo

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 代码之后


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

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

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

读书人网 >asp.net

热点推荐