C# 下拉列表选定索引更改在更新面板中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16439037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:00:26  来源:igfitidea点击:

Drop Down List Selected Index changed not working in Update panel

c#webformsupdatepanel

提问by Mathematics

I have a drop down list in UpdatePanel_2, it gets populated when Button_1 is clicked in UpdatePanel_1.

我在 UpdatePanel_2 中有一个下拉列表,当在 UpdatePanel_1 中单击 Button_1 时,它会被填充。

My ddlist markup is,

我的 ddlist 标记是,

<asp:DropDownList id="drop1" runat="server"  EnableViewState="true" AutoPostBack="true" OnSelectedIndexChanged="Drop1_SelectedIndexChanged" />

then code behind is,

那么后面的代码是,

 protected void Drop1_SelectedIndexChanged(object sender, EventArgs e)
        { }

I also tried putting AutoPostback=true to my DropDownList, still no success.

我也尝试将 AutoPostback=true 放入我的 DropDownList,但仍然没有成功。

I also added triggre to update panel 2 but no gain,

我还添加了触发器来更新面板 2 但没有增益,

       <Triggers>
    <asp:AsyncPostbackTrigger ControlID="drop1" EventName="SelectedIndexChanged" />
</Triggers>

I am populating DropDownList using a button not PAGE LOAD METHOD PLEASE READ before answering. Thanks

我正在使用按钮而不是页面加载方法填充 DropDownList,请在回答前阅读。谢谢

采纳答案by Neeraj Dubey

Check the data to populate the DropDownListin the Page_Loadevent and always check IspostBack:

检查数据来填充DropDownListPage_Load事件,并经常检查IspostBack

if(!IsPostBack)
{
 //DropDownList configuration
}

Use EnableViewState:

使用EnableViewState

 <asp:DropDownList ID="ddlAddDepPlans" runat="server" AutoPostBack="true" EnableViewState="true" />

Hope it helps you.

希望对你有帮助。

回答by Alberto León

Please, when you initialize it in Page_Load() check if not is postback. If you don't do it, you will always set the default value, and this replaces the value setted in the event.

请在 Page_Load() 中初始化它时检查是否不是回发。如果不这样做,您将始终设置默认值,这将替换事件中设置的值。

if(!IsPostBack)
{
//DropDownList configuration
}

回答by Jboy Flaga

I had the same issue. My problem was that the values of my ListItems were all the same :D

我遇到过同样的问题。我的问题是我的 ListItems 的值都是一样的:D

<asp:DropDownList ID="ddlFilterLogins" runat="server" Visible="true" AutoPostBack="true">
    <asp:ListItem Value="0" Text="All"></asp:ListItem>
    <asp:ListItem Value="0" Text="Some"></asp:ListItem>
    <asp:ListItem Value="0" Text="Some more"></asp:ListItem>
</asp:DropDownList>

It should be like this:

应该是这样的:

<asp:DropDownList ID="ddlFilterLogins" runat="server" Visible="true" AutoPostBack="true">
    <asp:ListItem Value="0" Text="All"></asp:ListItem>
    <asp:ListItem Value="1" Text="Some"></asp:ListItem>
    <asp:ListItem Value="2" Text="Some more"></asp:ListItem>
</asp:DropDownList>

Hope this helps. This might be hard to find sometimes :)

希望这可以帮助。这有时可能很难找到:)

回答by Chakri

You can use Init event instead of SelectIndexChanged. It worked fine for me. Hope you got my point.

您可以使用 Init 事件代替 SelectIndexChanged。它对我来说很好。希望你明白我的意思。

回答by Reagan

It was also a wired problem for me. finally It was because of identical listitems in the dropdown as shown below. during development you may use same items just for testing. change them.

这对我来说也是一个有线问题。最后是因为下拉列表中的列表项相同,如下所示。在开发过程中,您可以使用相同的项目进行测试。改变它们。

<asp:ListItem>Business</asp:ListItem>
<asp:ListItem>Business</asp:ListItem>
<asp:ListItem>Business</asp:ListItem>
<asp:ListItem>Business</asp:ListItem>