2011-06-14 73 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动态添加到一个占位符可以工作。

相关问题