加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 下拉列表重置SelectedValue

发布时间:2020-12-16 07:37:36 所属栏目:asp.Net 来源:网络整理
导读:我有一个连接到ObjectDataSource的DropDownList.在我的页面加载中,我根据具体情况将s??electedvalue设置为特定值.如何在我的方法中选择索引以“重置”所选值,以便您仍然可以从其他选项中进行选择?或者我不应该使用selectedvalue来设置下拉列表的默认值? as
我有一个连接到ObjectDataSource的DropDownList.在我的页面加载中,我根据具体情况将s??electedvalue设置为特定值.如何在我的方法中选择索引以“重置”所选值,以便您仍然可以从其他选项中进行选择?或者我不应该使用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;
}

解决方法

你应该只对DataDind你的DropDownList并设置它的默认值if(!IsPostBack):

protected void Page_Load(Object sender,EventArgs e)
{
    if(!IsPostBack)
    {
        int default = category.TypeId;
        DropDownList.SelectedValue = default.ToString();
    }
}

Page.IsPostBack Property

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读