private void loadDropLis()
{
DropDListBigType.DataTextField = "ProductBigTypeName";
DropDListBigType.DataValueField = "ProductBigTypeId";
DropDListBigType.DataSource = typebll.GetAllList();
DropDListBigType.DataBind();
DropDListSmalType.DataTextField = "ProductsTypeName";
DropDListSmalType.DataValueField = "ProductsTypeId";
DropDListSmalType.DataSource = typebll.GetSmallType(Convert.ToInt32(DropDListBigType.SelectedValue)).Tables[0];
DropDListSmalType.DataBind();
}
程序:
loadDropLis();调用绑定方法
DropDListBigType.SelectedValue = typebll.GetBigType(model.ProductsTypeId).Tables[0].Rows[0]["ProductBigTypeName"].ToString();报错但是string a=typebll.GetBigType(model.ProductsTypeId).Tables[0].Rows[0]["ProductBigTypeName"].ToString();可以取到值。前面我也绑定了?我想先把所有项加载出来。然后默认选中数据库中邪的项。希望指出原因。
{
DropDListBigType.DataTextField = "ProductBigTypeName";
DropDListBigType.DataValueField = "ProductBigTypeId";
DropDListBigType.DataSource = typebll.GetAllList();
DropDListBigType.DataBind();
DropDListSmalType.DataTextField = "ProductsTypeName";
DropDListSmalType.DataValueField = "ProductsTypeId";
DropDListSmalType.DataSource = typebll.GetSmallType(Convert.ToInt32(DropDListBigType.SelectedValue)).Tables[0];
DropDListSmalType.DataBind();
}
程序:
loadDropLis();调用绑定方法
DropDListBigType.SelectedValue = typebll.GetBigType(model.ProductsTypeId).Tables[0].Rows[0]["ProductBigTypeName"].ToString();报错但是string a=typebll.GetBigType(model.ProductsTypeId).Tables[0].Rows[0]["ProductBigTypeName"].ToString();可以取到值。前面我也绑定了?我想先把所有项加载出来。然后默认选中数据库中邪的项。希望指出原因。
if (DropDListBigType.Items.FindByValue("") != null)
{
DropDListBigType.SelectedValue = typebll.GetBigType(model.ProductsTypeId).Tables[0].Rows[0]["ProductBigTypeName"].ToString();
}
这样?
DropDListBigType.Items[0].Selected=true;//加上一个默认值。
因为你下面就使用 了DropDListBigType.SelectedValue
DropDListBigType.DataTextField = "ProductBigTypeName";
DropDListBigType.DataValueField = "ProductBigTypeId";
DropDListBigType.DataSource = typebll.GetAllList();
DropDListBigType.DataBind();
DropDListBigType.Items[0].Selected = true;
typebll.GetBigType(model.ProductsTypeId).Tables[0].Rows[0]["ProductBigTypeName"].ToString();这个值并不在DropDListBigType的value列表内导致的
ListItem x = DropDListBigType.Items.FindByValue(typebll.GetBigType(model.ProductsTypeId).Tables[0].Rows[0]["ProductBigTypeName"].ToString())
if(x!=null)
x.Selected=true;