2014-03-12 126 views
0

我有兩個列表框和一個按鈕。我需要通過單擊按鈕將所選項從一個列表框添加到另一個列表框。如何將項目從一個列表框添加到另一個窗體上

enter image description here

,這裏是按鈕

protected void ASPxButton4_Click(object sender, EventArgs e) 
    { 
     if (listBoxSubeKiyaslama1.SelectedIndex > -1) 
     { 
      listBoxSubeKiyaslama2.Items.Add(listBoxSubeKiyaslama1.SelectedItem); 
      listBoxSubeKiyaslama2.Items.RemoveAt(listBoxSubeKiyaslama1.SelectedIndex); 
      listBoxSubeKiyaslama2.UnselectAll(); 
     } 
    } 

當我按一下按鈕,我看到listBoxSubeKiyaslama1.SelectedIndex始終是 「-1」 的代碼。因爲我認爲它回發並清除第一個列表框中的項目。我怎樣才能解決這個問題?

+0

可能的重複[如何防止頁面被回發時,我將項目從一個列表框轉移到其他](http://stackoverflow.com/questions/7174880/how-to-prevent-page-being-postbacked-wheni-i-轉移項目從一個列表框到ot) – richaux

回答

1

你可以嘗試下面的代碼:

if (listBoxSubeKiyaslama1.SelectedItem != null) 
    { 
     listBoxSubeKiyaslama2.Items.Add(listBoxSubeKiyaslama1.SelectedItem); 
     listBoxSubeKiyaslama2.Items.RemoveAt(listBoxSubeKiyaslama1.SelectedIndex); 
     listBoxSubeKiyaslama2.UnselectAll(); 
    } 

該控件將不若EnableViewState = false上回發保存的值。默認情況下它是真的。請確保你沒有設置爲false

我也建議你把你的控制在UpdatePanel,以避免完全回發。

像:

<asp:UpdatePanel ID="up1" runat="Server"> 
    <ContentTemplate> 
     <asp:ListBox ID="listBoxSubeKiyaslama1" runat="server"> 
     </asp:ListBox> 
     <asp:ListBox ID="listBoxSubeKiyaslama2" runat="server"> 
     </asp:ListBox> 
    </ContentTemplate> 
</asp:UpdatePanel> 
+0

我讓它EnableViewState = true,但它仍然無法正常工作。這很奇怪 –

+0

updatePanel不工作要麼 –

+0

請按照教程:http://www.codeproject.com/Tips/570319/ASP-NET-Move-Items-from-One-Listbox-to-Another-Lis –

1

試試下面的代碼: -

ASPX.CS

public string GetSelectedItems(ListBox control) 
    { 
     var items = new StringBuilder(); 
     foreach (ListItem item in control.Items) 
     { 
      if (item.Selected) 
       items.Append(string.Format("{0},", item.Value)); 
     } 
     return items.ToString().Trim().TrimEnd(','); 
    } 
    protected void btnMoveRight_Click(object sender, EventArgs e) 
    { 
     for (int i = lbCourses1.Items.Count - 1; i >= 0; i--) 
     { 
      if (lbCourses1.Items[i].Selected == true) 
      { 
       lbCourses2.Items.Add(lbCourses1.Items[i]); 
       ListItem li = lbCourses1.Items[i]; 
       lbCourses1.Items.Remove(li); 
      } 
     } 
    } 
    protected void btnMoveLeft_Click(object sender, EventArgs e) 
    { 
     for (int i = lbCourses2.Items.Count - 1; i >= 0; i--) 
     { 
      if (lbCourses2.Items[i].Selected == true) 
      { 
       lbCourses1.Items.Add(lbCourses2.Items[i]); 
       ListItem li = lbCourses2.Items[i]; 
       lbCourses2.Items.Remove(li); 
      } 
     } 
    } 

    var selectedValues = GetSelectedItems(lb2); 

ASPX

<asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
<ContentTemplate> 
<div> 
      <asp:Label ID="lbl1" runat="server" Text="lbl1:"></asp:Label> 
      <asp:ListBox ID="lb1" runat="server" SelectionMode="Multiple"></asp:ListBox> 
      <asp:Button Runat="server" ID="btnMoveRight" Text=">>" 
      onclick="btnMoveRight_Click"></asp:Button> 
      <asp:Button Runat="server" ID="btnMoveLeft" Text="<<" 
      onclick="btnMoveLeft_Click"></asp:Button> 
      <asp:ListBox ID="lb2" runat="server" SelectionMode="Multiple"></asp:ListBox> 
     </div> 
</ContentTemplate> 
</asp:UpdatePanel> 
相關問題