我的数据库中有 4 个对象,例如 zen、maruthi 和 scorpio ..将值绑定到下拉列表后,我只能看到 scorpio 重复 3 次..
而不是把它当作
zen
maruthi
scorpio
..我得到天蝎座
scorpio
scorpio..
代码
List<Cab> CabTypeList = new List<Cab>();
using (DataTable table = SqlDBConnector.ExecuteSelectCommand("GetCabType", CommandType.StoredProcedure))
{
//check if any record exist or not
if (table.Rows.Count > 0)
{
//Lets go ahead and create the list of cab
foreach (DataRow row in table.Rows)
{
cab.CabType = row["CabType"].ToString();
cab.CabId = Convert.ToInt32(row["Cab_Id"]);
CabTypeList.Add(cab);
}
}
}
ASPX 页面
if (!IsPostBack)
{
CabDbAccess cabdbaccess = new CabDbAccess();
DropDownList1.DataSource = cabdbaccess.GetCabType();
DropDownList1.DataTextField = "CabType"; // the items to be displayed in the list items
DropDownList1.DataValueField = "CabId"; // the id of the items displayed
DropDownList1.DataBind();
}