我有一个连接到ObjectDataSource的DropDownList。在我的页面加载中,我根据情况将selected值设置为一个特定值。我如何在我的方法selectedindexchanged中“重置”selected值,这样你仍然可以从其他选项中进行选择?或者我不应该使用selectedvalue来设置dropdownlist的默认值?
<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;
}发布于 2012-04-12 19:36:57
您应该只对DropDownList执行DataBind操作,并将其缺省值设置为if(!IsPostBack)
protected void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
int default = category.TypeId;
DropDownList.SelectedValue = default.ToString();
}
}Page.IsPostBack Property
发布于 2012-04-12 20:20:45
下拉列表可以使用ClearSelection方法
DropDownList.ClearSelection();谢谢
深度
发布于 2012-04-12 20:06:48
将SelectedValue属性设置为空字符串:
DropDownList.SelectedValue = string.Empty;https://stackoverflow.com/questions/10122696
复制相似问题