Asp.net updatepanel Listbox not refreshing layout

我与影子孤独终老i 提交于 2019-12-08 12:32:57

问题


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);
        }
    }
}

回答1:


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>



回答2:


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);

        }
    }


来源:https://stackoverflow.com/questions/12384747/asp-net-updatepanel-listbox-not-refreshing-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!