简体   繁体   中英

ASP.NET DropDownList SelectedIndexChanged with UpdatePanel AsyncPostBackTrigger

My DDL doesn't work with SelectedIndexChanged, or rather it only worked for the first time. Second time onward it doesn't trigger the drpItemType_SelectedIndexChanged method anymore.

ASPX

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:DropDownList ID="drpItemType" runat="server" CssClass="drpDown" Width="370px" OnSelectedIndexChanged="drpItemType_SelectedIndexChanged" AutoPostBack="true">
              <asp:ListItem Text="Computer (Desktop/Laptop)" Value="PC"></asp:ListItem>
              <asp:ListItem Text="Others" Value="Others"></asp:ListItem>
        </asp:DropDownList>
        <asp:Label ID="lblID1" runat="server" />
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="drpItemType" EventName="SelectedIndexChanged" />
    </Triggers>
</asp:UpdatePanel>

Code Behind

protected void drpItemType_SelectedIndexChanged(object sender, EventArgs e)
{
     if (drpItemType.SelectedValue == "PC")
     {
         lblID1.Text = "PC";
     }
     else if (drpItemType.SelectedValue == "Others")
     {
         lblID1.Text = "Others";
     }
}

Actually your code works fine. I copied it and pasted it into a new project and it runs fine every time. Try it yourself. I think you have something else on your page which is causing a JavaScript error, and stopping subsequent postbacks from working. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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