在绑定后,下拉列表显示以下值:
下拉列表在每次回发后显示以下值:
代码如下:
protected void Page_Load(object sender, EventArgs e)
{
DropDownList1.DataSource = db.ComplaintTypes.ToList();
DropDownList1.DataTextField = "ct_Name";
DropDownList1.DataBind();
cboCpriority.DataSource = db.ComplaintPriorities.ToList();
cboCpriority.DataTextField = "cp_Desc";
cboCpriority.DataBind();
...
}
答案 0 :(得分:5)
您应该使用IsPostBack属性仅在首次加载时绑定,因为在此之后,值将通过视图状态保留。
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DropDownList1.DataSource = db.ComplaintTypes.ToList();
DropDownList1.DataTextField = "ct_Name";
DropDownList1.DataBind();
cboCpriority.DataSource = db.ComplaintPriorities.ToList();
cboCpriority.DataTextField = "cp_Desc";
cboCpriority.DataBind();
}
}