您好我正在尝试在asp.net中实现下拉列表。下拉列表正在从数据库加载,并通过下拉列表进行绑定。该列表具有各种值和文本。但我想添加第一个选项"请选择"在下拉列表顶部禁用以供选择。同样在绑定下拉列表时,我想指向用户之前选择的值,这就是我使用selectedValue选项的原因。问题是如果我将第一个项目添加为"请选择"从项目0和选定值后面的代码到存储在db中的值,它仍然显示项目零"请选择"。
DropDownList ddlMinEdit = (DropDownList)currentGrid.Rows
[currentGrid.EditIndex].FindControl("ddlMinEdit");
ddlMinEdit.Items.Insert(0, "Select Please");
ddlMinEdit.Items [0].Attributes.Add("disabled", "disabled");
ddlMinEdit.DataSource = CList;
ddlMinEdit.DataTextField = "empid";
ddlMinEdit.DataValueField = "empname";
ddlMinEdit.DataBind();
ddlMinEdit.SelectedValue = defemp;
所以任何建议如何添加第一个项目,同时绑定列表并指向选定的值。感谢
答案 0 :(得分:0)
你做事的顺序似乎不正确。你应该插入"选择请"在数据绑定和设置所选值之后:
DropDownList ddlMRCClinEdit = (DropDownList)currentGrid.Rows [currentGrid.EditIndex].FindControl("ddlMRCClinEdit");
ddlMRCClinEdit.DataSource = clinList;
ddlMRCClinEdit.DataTextField = "empid";
ddlMRCClinEdit.DataValueField = "empname";
ddlMRCClinEdit.DataBind();
ddlMRCClinEdit.SelectedValue = defemp;
ddlMRCClinEdit.Items.Insert(0, "Select Please");
ddlMRCClinEdit.Items [0].Attributes.Add("disabled", "disabled");