2011-06-14 72 views
1

大師,如何產生和處理多個checklistboxes

我一直對我怎麼能去處理多個,改變與C#中的ASP.NET項目類別尋找一個明顯的例子。該數據類似於以下內容:

Category1 Heading 
    Item 
    Item 
    Item 

Category2 Heading 
    Item 
    Item 
    Item 
Category3 Heading 
    Item 
    Item 
    Item 

類別名稱和項來自SQL數據庫。我想我需要一個項目的每個類別的複選框列表和類別的數量是動態的,並會隨着時間而改變。是否有可能創建一個循環來動態構建所需的checkboxlists,然後能夠處理它們?我閱讀了關於也許使用數據中繼器。任何援助非常感謝。

回答

0

您可以使用中繼器或DataGrid。有時候我更容易使用DataGrid併爲每個新類別添加行。你有什麼更詳細的信息你想要用它做什麼?

您可以使用DataGrid併爲每個類別添加一個新行。並在該行內添加控件。不太明智的方法是在頁面上使用PlaceHolder,並將控件添加到該控件中。但是我肯定會建議一個DataGrid或者直視Repeater。

+0

感謝您的快速回復。我需要收集收集的數據並將其插入數據庫。我現在使用的單個清單框可用於處理,但不允許對各個項目進行分組。我需要將成串的項目分組,並從數據庫中爲它們提供標題。 – techguy817 2011-06-14 22:08:51

0

你可以嘗試這樣的事情......

你的業務對象。你可以使用這個作爲一種模式,你可以從你的「真正的」業務對象變換或使用你的業務對象直接

public class BusinessObject 
{ 
    public string Category { get; set; } //Your category 
    public int ID { get; set; }   //Point of data entry and will be return on post 
    public string Name { get; set; }  //A friendly name for your users 
} 

你的ASPX標記。我使用的轉發器只有一個CheckBoxList其中將有實際項目的類別。這可以擴展和風格相當多。

<asp:Repeater ID="myRepeater" runat="server"> 
    <ItemTemplate> 
     <asp:CheckBoxList ID="checkboxlist" 
          runat="server" 
          DataTextField="Name" 
          DataValueField="ID" /> 
    </ItemTemplate> 
</asp:Repeater> 

一個地方來獲取您的業務對象:在這裏,我只是在我的代碼隱藏的成員。您應該從業務層/層獲取這些數據。

List<BusinessObject> MyBusinessObjects = new List<BusinessObject>(); 

,並提前許多項目有怎麼可能是你的背後

protected void Page_Load(object sender, EventArgs e) 
    { 
     //Wire up the event to handle when items are bound to the repeater 
     this.myRepeater.ItemDataBound += new RepeaterItemEventHandler(myRepeater_ItemDataBound); 
     //Now actually bind the categories to the repeater 
     this.myRepeater.DataSource = GetCategories(MyBusinessObjects); 
     this.myRepeater.DataBind(); 
    } 

    void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e) 
    { 
     //Don't process items that are not item, or alternating item 
     if (!(e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)) return; 
     //Grab a reference to the checkboxlist in our repeater 
     CheckBoxList checkboxlist = (CheckBoxList)e.Item.FindControl("checkboxlist"); 
     //Now put our business objects of that category in it 
     checkboxlist.DataSource = GetItemsFromCategory(MyBusinessObjects, (string)e.Item.DataItem); 
     checkboxlist.DataBind(); 
    } 
    //Helper to grab categories. 
    private IEnumerable<string> GetCategories(IEnumerable<BusinessObject> items) 
    { 
     return (from i in items 
       select i.Category).Distinct(); 
    } 
    //Helper to grab the items in categories. 
    private IEnumerable<BusinessObject> GetItemsFromCategory(IEnumerable<BusinessObject> items, string category) 
    { 
     return (from i in items 
       where i.Category == category 
       select i); 
    } 
+0

所以它聽起來像有幾個選項:中繼器,帶模板的listview,業務對象。不確定哪一個最簡單。 – techguy817 2011-06-15 00:15:06

+0

編程的美妙之處在於它們有很多選擇。我在這裏建議的解決方案只是一種方法。大多數編碼人員會說最容易維護的解決方案;不一定最容易構建。 – Jay 2011-06-15 13:45:59

+0

我看着數據網格,但我不知道如何使用複選框。我需要用戶選擇他們想要的任何項目組合,並將這些信息記錄在數據庫中。 – techguy817 2011-06-17 00:33:06

0

如果你不知道的代碼,那麼你需要的是不會打破的顯示。實現這一目標的最好方法是使用一個ListView與GroupTemplate:

http://forums.asp.net/t/1364813.aspx/1

+0

仍然試圖找出這一個(我一般相當新的.NET)。我知道在頁面加載時數據庫中會有多少項目,它不會被用戶動態更改。用戶將只選擇他們想要的課程。我只需要找到一種將物品分組的方法。 – techguy817 2011-06-19 22:13:05

+0

只要使用作爲本文的結尾描述分組:http://msdn.microsoft.com/en-us/magazine/cc337898.aspx – IrishChieftain 2011-06-19 22:57:47

+0

好像分組選項只是指定項目組的具體數量在一起。這不會幫助我,因爲我需要按類別將各種項目組合在一起。例如,第一部分可能有4個項目,接下來的6個項目等等,因爲它來自數據庫。 – techguy817 2011-06-20 03:19:00

0

試想一下,這是你的數據結構。做沒有中繼器。

 
var list = new Dictionary<string, List<string>> 
          { 
           {"Name1", new List {"item1", "item2", "item3"}}, 
           {"Name2", new List {"item1", "item2", "item3"}}, 
           {"Name3", new List {"item1", "item2", "item3"}}, 
           {"Name4", new List {"item1", "item2", "item3"}}, 
           {"Name5", new List {"item1", "item2", "item3"}} 
          }; 

      foreach (var category in list) 
      { 
       var checkBoxList = new CheckBoxList 
             { 
              Text = category.Key 
             }; 

       foreach (var value in category.Value) 
       { 
        var listItem = new ListItem(value); 
        checkBoxList.Items.Add(listItem); 
       } 
      } 
0

再次感謝大家的幫助。我們最終改變了需求,現在一個checkboxlist就足夠了。我認爲checkboxlists動態添加到一個佔位符可以工作。

相關問題