1

グル、

ASP.NET で C# を使用して複数のさまざまなカテゴリのアイテムを処理する方法について、明確な例を探していました。データは次のようになります。

Category1 Heading
  Item
  Item
  Item

Category2 Heading
  Item
  Item
  Item
Category3 Heading
  Item
  Item
  Item

カテゴリ名と項目は、SQL データベースから取得されます。項目のカテゴリごとに 1 つのチェックボックス リストが必要になると思います。カテゴリの数は動的であり、時間の経過とともに変化します。ループを作成して必要なチェックボックス リストを動的に作成し、それらを処理できるようにすることは可能ですか? おそらくデータリピーターの使用についても読みました。どんな支援も大歓迎です。

4

5 に答える 5

0

これがあなたのデータ構造だと想像してみてください。リピーターなしで行う。

var list = new Dictionary <string、List <string >>
                           {{
                               {"Name1"、新しいリスト{"item1"、 "item2"、 "item3"}}、
                               {"Name2"、新しいリスト{"item1"、 "item2"、 "item3"}}、
                               {"Name3"、新しいリスト{"item1"、 "item2"、 "item3"}}、
                               {"Name4"、新しいリスト{"item1"、 "item2"、 "item3"}}、
                               {"Name5"、新しいリスト{"item1"、 "item2"、 "item3"}}
                           };

            foreach(リスト内のvarカテゴリ)
            {{
                var checkBoxList = new CheckBoxList
                                       {{
                                           テキスト=category.Key
                                       };

                foreach(category.Valueのvar値)
                {{
                    var listItem = new ListItem(value);
                    checkBoxList.Items.Add(listItem);
                }
            }
于 2011-06-14T22:18:12.080 に答える
0

アイテムがいくつあるか事前にわからない場合は、壊れないディスプレイが必要です。これを実現する最善の方法は、GroupTemplate で ListView を使用することです。

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

于 2011-06-14T22:13:01.583 に答える
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>

ビジネス オブジェクトを取得する場所: コード ビハインドにメンバーが 1 つあります。このデータは、ビジネス レイヤー/層から取得する必要があります。

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);
    }
于 2011-06-14T22:12:56.570 に答える
0

リピーターまたは DataGrid を使用できます。場合によっては、DataGrid を使用して、新しいカテゴリごとに行を追加する方が簡単です。それを使って何をしようとしているのかについて、さらに具体的なことはありますか?

DataGrid を使用して、カテゴリごとに新しい行を追加できます。そして、その行内に Controls を追加します。あまり知識のない方法は、ページで PlaceHolder を使用し、それにコントロールを追加することです。しかし、私は間違いなく DataGrid を提案するか、Repeater を調べることをお勧めします。

于 2011-06-14T22:01:20.537 に答える
0

助けてくれてありがとう。最終的に要件を変更し、単一のチェックボックス リストで十分になりました。ただし、プレースホルダーへのチェックボックスリストの動的な追加はうまくいったと思います。

于 2011-06-23T21:05:23.377 に答える