【发布时间】:2025-11-25 03:40:01
【问题描述】:
我有两个列表框,其中一个是源,另一个是目标。
我想在按钮点击事件后将选中的项目转移到目标列表框。
我在互联网上搜索并找到了来自here 的样本。但就我而言,它不起作用。
我的 ASP 源文件是:
<asp:Table ID="tbvl" Width="100%" runat="server">
<asp:TableRow>
<asp:TableCell Width="45%">
<asp:ListBox ID="lstsource" CssClass="uppercase" runat="server" Width="100%" Height="140" SelectionMode="Multiple"></asp:ListBox>
</asp:TableCell>
<asp:TableCell Width="10%" HorizontalAlign="Center" Height="100%">
<asp:Table ID="Table1" runat="server" Height="100%">
<asp:TableRow>
<asp:TableCell Height="25%"><asp:Button ID="btnsd" CssClass="button" runat="server" Text=">>" Width="40" /></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell Height="25%"><asp:Button ID="btnds" runat="server" CssClass="button" Text="<<" Width="40" /></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell Height="25%"><asp:Button ID="btnallsd" runat="server" CssClass="button" Text=">" Width="40" /></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell Height="25%"><asp:Button ID="btnallds" runat="server" CssClass="button" Text="<" Width="40" /></asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:TableCell>
<asp:TableCell Width="50%">
<asp:ListBox ID="lstdestination" runat="server" CssClass="uppercase" Width="100%" Height="140" SelectionMode="Multiple"></asp:ListBox>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
而我的Click() 事件是;
void btnallsd_Click(object sender, EventArgs e)
{
for (int i = lstsource.Items.Count - 1; i >= 0; i--)
{
if (lstsource.Items[i].Selected)
{
lstdestination.Items.Add(lstsource.Items[i]);
lstdestination.ClearSelection();
lstsource.Items.Remove(lstsource.Items[i]);
}
}
}
当我单击按钮时,页面只会刷新,但项目并未添加到目标列表框中。
我应该怎么做才能得到正确的输出。
请帮忙。
【问题讨论】:
-
当页面被刷新时,两个列表框也会被刷新并且回发的原因会出现这个问题。首先确保这一点。
-
我应该做些什么改变..?你能指定@harhar
-
可以使用ajax更新面板控件。
标签: c# asp.net listboxitems aspbutton