简体   繁体   English

将DropDown的SelectedIndex设置为0,并将AutoPostBack设置为true

[英]Setting SelectedIndex to 0 for DropDown with AutoPostBack set to true

I am using a DropDown button with AutoPostBack set to true. 我正在使用将AutoPostBack设置为true的DropDown按钮。 I'm trying to reset the DropDown to the default selection on page postback -- DropDown.SelectedIndex = 0;. 我正在尝试将DropDown重置为页面回发时的默认选择-DropDown.SelectedIndex = 0;。 The DropDown does reset to the default selection but I'm unable to make a selection as it continuously reverts to the default position "Make a Selection" immediately after selecting another option. DropDown确实重置为默认选择,但我无法进行选择,因为它在选择另一个选项后会立即连续恢复到默认位置“进行选择”。 I'm not sure if it matters but this is placed inside an update panel. 我不确定是否重要,但这位于更新面板中。

One of the reasons I need this to set back to 0 is that when I click the back button on the browser, the last selection is selected and if the user tries to go back to the same selection, that particular selection in the DropDown doesn't work unless I refresh the page. 我需要将其设置为0的原因之一是,当我单击浏览器上的“后退”按钮时,选择了最后一个选择,并且如果用户尝试返回到同一选择,则DropDown中的特定选择不会除非刷新页面,否则不起作用。 For example. 例如。 Choice 1 redirects me to page2. 选择1将我重定向到page2。 If I click back, Choice 1 is still selected. 如果我单击返回,则选择1仍处于选中状态。 If I click the drop down to select Choice 1 again, nothing happens unless I refresh the page. 如果单击下拉菜单再次选择选项1,则除非刷新页面,否则什么都不会发生。 Here is my DropDown HTML: 这是我的DropDown HTML:

<asp:DropDownList ID="DropDown" runat="server" EnableViewState="true" 
AppendDataBoundItems="true" AutoPostBack="true" 
OnSelectedIndexChanged="DropDown_SelectedIndexChanged">
                    <asp:ListItem>Make a Selection</asp:ListItem>
                    <asp:ListItem>Choice 1</asp:ListItem>
                    <asp:ListItem>Choice 2</asp:ListItem>
                </asp:DropDownList>

And C#: 和C#:

if (IsPostBack)
        {                
            DropDown.SelectedIndex = 0;

        }

Based on what you are experiencing, I think it's your browser what's restoring your control state when you use the browser back button. 根据您的经验,当您使用浏览器的后退按钮时,我认为是浏览器还原了控制状态。

A way to avoid this behavior is by adding the autocomplete="off" to your <form> element, for example: 避免此行为的一种方法是,将autocomplete="off"<form>元素,例如:

<form id="form1" runat="server" autocomplete="off">
...
</form>

If adding the autocomplete="off" alone is not enough, also tell the browser to not cache the page by sending the following headers, for example, from your Page_Load method: 如果仅添加autocomplete="off"是不够的,还可以通过发送以下标头(例如从您的Page_Load方法发送)来告诉浏览器不要缓存页面:

protected void Page_Load(object sender, EventArgs e)
{
    Response.Cache.SetNoStore();
    Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
}

Remember to remove the line doing the DropDown.SelectedIndex = 0; 记住删除DropDown.SelectedIndex = 0;DropDown.SelectedIndex = 0;的那一行DropDown.SelectedIndex = 0; before trying this solution. 在尝试此解决方案之前。

There are couple of reasons: 有两个原因:

  1. First need to remove: 首先需要删除:

    if (IsPostBack) { 如果(IsPostBack){
    DropDown.SelectedIndex = 0; DropDown.SelectedIndex = 0;
    } }

As you have AutoPostback="true" enabled, page_load will be called along with the OnSelectedIndexChanged event which is changing the SelectedIndex to 0. 启用AutoPostback="true" ,将调用page_load以及OnSelectedIndexChanged事件,该事件会将SelectedIndex更改为0。

  1. Also, need to ensure Enable.ViewState property is set to true. 另外,需要确保Enable.ViewState属性设置为true。

Please use the following code in page load 请在页面加载中使用以下代码

if (!IsPostBack)
{                
    DropDown.SelectedIndex = 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM