简体   繁体   中英

Asp.net updatepanel Listbox not refreshing layout

so my problem is fairly simple i have 2 list-boxes and one button. when an item is selected in listbox2 and the button is clicked i would like to add the selected item to listbox1.

And it all seams to work until I add the second item. then listbox1 doesn't refresh it's items...

<body>
<form id="form1" runat="server">
    <asp:ScriptManager runat="server"></asp:ScriptManager>
    <div>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
            <asp:ListBox ID="ListBox2" runat="server"></asp:ListBox>
                <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ListBox2.Items.Add(new ListItem("1", "1"));
            ListBox2.Items.Add(new ListItem("2", "2"));
            ListBox2.Items.Add(new ListItem("3", "3"));
            ListBox2.Items.Add(new ListItem("4", "4"));
            ListBox2.Items.Add(new ListItem("5", "5"));
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (ListBox2.SelectedIndex != -1)
        {
            ListBox1.Items.Add(ListBox2.SelectedItem);
        }
    }
}

You can add UpdateMode=conditional to your UpdatePanel

And set <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />

  <asp:ScriptManager runat="server"></asp:ScriptManager>
    <div>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
                <asp:ListBox ID="ListBox2" runat="server"></asp:ListBox>
                <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
            </ContentTemplate>

            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
            </Triggers>

        </asp:UpdatePanel>
    </div>

Found the solution :) The problem was that Listbox1 SelectionMode = Single... so when the result is returned the error is on the client side. solution is to deselect the item before you add it to the other list. (or make a new with the value and text from the selected one)

 protected void Button1_Click(object sender, EventArgs e)
    {
        if (ListBox2.SelectedIndex != -1)
        {

            ListItem item = ListBox2.SelectedItem;
            item.Selected = false;
            ListBox1.Items.Add(item);

        }
    }

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