简体   繁体   中英

Listbox OnSelectedIndexChanged doesn't work

I have a listbox inside my jquery dialog,

<div id="dialog">
<asp:UpdatePanel ID="updatePanel1" runat="server">
<ContentTemplate>
<asp:ListBox ID="lbxTitle" runat="server" style="width:400px;height:100px;" OnSelectedIndexChanged="lbxTitle_SelectedIndexChanged">
    <asp:ListItem value="2" Text="abc" />
    <asp:ListItem value="3" Text="def" />
</asp:ListBox>
<br />
<fieldset>
    <asp:Label ID="lblContent" runat="server" style="height:200px;" />
</fieldset>
</ContentTemplate>
</asp:UpdatePanel>
</div>

And in the behind code, I have this function

protected void lbxTitle_SelectedIndexChanged(object sender, EventArgs e)
{
   lblContent.Text = "test";
}

I dont put autopostback="true" because it will refresh the whole page, and close my dialog.

How to make it so that when user click item on the listbox, it will show "test" on the label inside dialog.

Now, it doesnt do anything if I change the selected item.

You should put the Autopostback then you can use AsyncPostBack to prevent whole page postback...

As per the following Code

<asp:UpdatePanel ID="updatePanel1" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:ListBox ID="lbxTitle" runat="server" style="width:400px;height:100px;" OnSelectedIndexChanged="lbxTitle_SelectedIndexChanged" AutoPostBack="true">
                <asp:ListItem value="2" Text="abc" />
                <asp:ListItem value="3" Text="def" />
            </asp:ListBox>
            <br />
            <fieldset><asp:Label ID="lblContent" runat="server" style="height:200px;" />
            </fieldset>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="lbxTitle" />
        </Triggers>
    </asp:UpdatePanel>

Add AutoPostback and after you create your dialog move it back to the form:

$("#dialog").parent().appendTo($("form:first"));

Try this:

<script type="text/javascript">

    $(function() {
        <% if (Page.IsPostback) { %>
            $('id of your listbox').change();
        <% } %>
    });

</script>

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