[英]Asp.net updatepanel Listbox not refreshing layout
so my problem is fairly simple i have 2 list-boxes and one button. 所以我的问题很简单,我有2个列表框和一个按钮。 when an item is selected in listbox2 and the button is clicked i would like to add the selected item to listbox1.
当在listbox2中选择一个项目并单击该按钮时,我想将所选项目添加到listbox1。
And it all seams to work until I add the second item. 在我添加第二个项目之前,所有接缝都能正常工作。 then listbox1 doesn't refresh it's items...
然后listbox1不刷新它的项目......
<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
您可以将
UpdateMode=conditional
添加到UpdatePanel
And set <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
并设置
<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. 找到解决方案:)问题是Listbox1 SelectionMode = Single ...所以当返回结果时,错误在客户端。 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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.