我有一个连接到ObjectDataSource的DropDownList。在我的页面加载中,我根据具体情况将selectedvalue设置为特定值。如何在我的方法中选择索引以“重置”所选值,以便您仍然可以从其他选项中进行选择?或者我不应该使用selectedvalue来设置下拉列表的默认值吗?
<asp:DropDownList ID="DropDownList" runat="server" DataSourceID="ObjectDataSource" DataTextField="Type" DataValueField="TypeId"
AutoPostBack="true" onselectedindexchanged="DropDownList_SelectedIndexChanged" >
</asp:DropDownList>
protected void Page_Load(object sender, EventArgs e)
{
int default = category.TypeId;
DropDownList.SelectedIndex = default;
}
protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
int id = int.Parse(DropDownList.SelectedValue);
Session["id"] = id;
}
答案 0 :(得分:23)
您可以将DropSelection方法用于下拉列表
DropDownList.ClearSelection();
由于
迪普
答案 1 :(得分:2)
您应DataBind
DropDownList
if(!IsPostBack)
并设置其默认值protected void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
int default = category.TypeId;
DropDownList.SelectedValue = default.ToString();
}
}
:
{{1}}
答案 2 :(得分:1)
将SelectedValue属性设置为空字符串:
DropDownList.SelectedValue = string.Empty;
答案 3 :(得分:0)
您可以将SelectedIndex设置为-1
DropDownList.SelectedIndex = -1;