【问题标题】:Asp.net updatepanel Listbox not refreshing layoutAsp.net updatepanel 列表框不刷新布局
【发布时间】:2012-09-12 09:09:18
【问题描述】:

所以我的问题很简单,我有 2 个列表框和一个按钮。 当在 listbox2 中选择一个项目并单击按钮时,我想将所选项目添加到 listbox1。

在我添加第二个项目之前,所有这些都可以正常工作。那么 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);
        }
    }
}

【问题讨论】:

    标签: c# asp.net ajax listbox asp.net-ajax


    【解决方案1】:

    您可以将UpdateMode=conditional 添加到您的UpdatePanel

    并设置&lt;asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" /&gt;

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

    【讨论】:

    • 使用此代码,您可以执行部分​​代码,而不是发布所有页面。
    • 任何 Ide 的关于如何修复它?第一项看起来很好它是第二个不...在调试模式下第二次单击我可以看到 listbox1 在添加第二个之前有一个...
    • 你可以坚持使用 ViewState ,你不需要重新绑定你的 ListBox1,EnableViewState 被默认设置为 true
    【解决方案2】:

    找到了解决方案:) 问题是 Listbox1 SelectionMode = Single... 所以当返回结果时 错误在客户端。解决方案是在将其添加到其他列表之前取消选择该项目。 (或使用所选值和文本新建一个)

     protected void Button1_Click(object sender, EventArgs e)
        {
            if (ListBox2.SelectedIndex != -1)
            {
    
                ListItem item = ListBox2.SelectedItem;
                item.Selected = false;
                ListBox1.Items.Add(item);
    
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-06
      • 2011-01-08
      • 2020-09-09
      • 2011-08-30
      相关资源
      最近更新 更多