1

がありDropDownList、リストの項目は次のとおりです。

Bedroom
Kitchen
Garden

したがって、上記のリストからいずれか 1 つの項目を選択するTextBoxと、番号が指定された があります。したがって、 で指定された数に応じて、TextBoxロケーションの数が に追加されますCheckBoxList

例えば、「ベッドルーム」を選択して数字を「3」とCheckBoxListすると、以下のようになります。

Bedroom1
Bedroom2
Bedroom3

私が直面している問題は、からランダムなアイテムを削除できるためCheckBoxList、アイテムがソートされないことです。たとえば、「Bedroom2」を削除すると、リスト アイテムは次のようになります。

Bedroom1  
Bedroom3  

再び「ベッドルーム」が選択されDropDownList、「2」などの数字が指定された場合、のアイテムは次のCheckBoxListようになります。

Bedroom1
Bedroom3
Bedroom1
Bedroom2

からアイテムを選択しても、これは同じ問題DropDownListです。

それで、誰かが親切にこれらのアイテムをどのように追加してもソートするのを手伝ってくれますか. の各場所に従ってソートする必要がありますDropDownList

4

2 に答える 2

7

これを試して

        var items = CheckBoxList1.Items
            .Cast<ListItem>()
            .OrderBy(i=>i.Text)
            .ToArray();
        CheckBoxList1.Items.Clear();
        CheckBoxList1.Items.AddRange(items);
于 2012-08-17T05:21:31.993 に答える
0

CheckBoxListに追加する前に、 ArrayListに項目を追加できます。次に、配列リストを並べ替えてから、DataSourceをCheckBoxListとDataBind()に渡します。

 if(!Page.IsPostBack)
 {  
        //Create two ArrayLists for sorting items
        ArrayList listItems1 = new ArrayList();
        ArrayList listItems2 = new ArrayList();

        int q1=Convert.ToInt16(TextBox1.Text);
        for (int i = 1; i <= q1; i++)
        { 
            string t1 = DroDownList1.SelectedItem.ToString().Trim();
            //Add items hereusing .Add() method of ArrayList
            listItems1.Add(string.Concat(t1, i));
            listItems2.Add(DropDownList1.SelectedItem.Text);
        }

        //Sort your items using .Sort() method of ArrayList
        listItems1.Sort()
        listItems2.Sort()

        //Bind your items to your CheckBoxLists
        CheckBoxList1.DataSource = listItems1;
        CheckBoxList2.DataSource = listItems2;  
        CheckBoxList1.DataBind();
        ChackBoxList2.DataBind();  
    } 

これは元のリンクです。

于 2012-08-17T05:22:58.153 に答える