2012-09-12 63 views
0

所以我的問題是相當簡單我有2個列表框和一個按鈕。 當在列表框2中選擇一個項目並單擊該按鈕時,我想將所選項目添加到列表框1。Asp.net updatepanel Listbox不刷新佈局

所有的接縫工作,直到我添加第二項。然後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); 
     } 
    } 
} 

回答

1

您可以添加UpdateMode=conditionalUpdatePanel

並設置<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> 
+0

但這不作listBox1中重繪其項目... – Skvettn

+0

有了這個代碼在執行部分的代碼,你不張貼所有你的頁面。 –

+0

任何Ide的如何解決它?第一個項目顯示罰款,這是第二個DOS不... 在調試模式下第二次點擊我可以看到,listbox1有一個項目之前,我添加第二個... – Skvettn

0

找到了解決辦法:) 問題是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); 

     } 
    }