【问题标题】:Dropdownlist Reset SelectedValue下拉列表重置 SelectedValue
【发布时间】:2012-04-12 11:32:34
【问题描述】:

我有一个连接到 ObjectDataSource 的 DropDownList。在我的页面加载中,我根据情况将 selectedvalue 设置为特定值。如何在我的方法 selectedindexchanged 中“重置” 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;
}

【问题讨论】:

  • 你能详细说明一下代码吗?

标签: asp.net drop-down-menu selectedvalue


【解决方案1】:

下拉列表可以使用 ClearSelection 方法

DropDownList.ClearSelection();

谢谢

深浦

【讨论】:

    【解决方案2】:

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

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

    Page.IsPostBack Property

    【讨论】:

      【解决方案3】:

      将 SelectedValue 属性设置为空字符串:

      DropDownList.SelectedValue = string.Empty;
      

      【讨论】:

        【解决方案4】:

        您可以将 SelectedIndex 设置为 -1

        DropDownList.SelectedIndex = -1;
        

        【讨论】:

          猜你喜欢
          • 2017-06-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-22
          相关资源
          最近更新 更多