天天看點

教學思路ASP.Net之伺服器控件:三、DropDownList、ListBox、CheckBoxList、RadioButtonList控件

       DropDownList控件類似于Winform中的combox下拉控件,如果想手工的添加DropDownList中的項目ListItem的方式如圖:

教學思路ASP.Net之伺服器控件:三、DropDownList、ListBox、CheckBoxList、RadioButtonList控件

      選中編輯項後出現ListItem集合編輯器,通過添加按鈕添加一個listItem項,同時我們會發現每項都會有四個屬性需要設定,Enable設定此項目是否可見;當Selected屬性=true時,頁面加載時DropDownList控件做選中的改項目,如果所有的項目的Selected的屬性都是False時,頁面加載後會預設選中第一個項目,這點與combox不同,combox在Winform中界面加載時,如果不設定選中的項,文本框内将沒有任何一項的Text;Text屬性設定了項目的顯示文本;Value屬性設定了此控件上每個項的Value值,也就是說每一項可以綁定兩個數值,Text屬性将顯示在控件上,下面這段代碼是頁面的源代碼:

1                                        <asp:DropDownList ID="DropDownList1" runat="server" >

2                                                 <asp:ListItem Enabled="False" Value="0">選項1</asp:ListItem>

3                                                 <asp:ListItem Selected="True" Value="1">選項2</asp:ListItem>

4                                                 <asp:ListItem Value="2">選項3</asp:ListItem>

5                                         </asp:DropDownList>

        選項1将不會顯示出來,因為Enable=“False”,頁面加載将顯示選項2,因為Selected="True",下面我們在頁面中加入一個Button,點選Button進入Button的Click事件訂閱的方法,寫如下代碼:

1     protected void Button1_Click(object sender, EventArgs e)

2         {

3                 Response.Write(DropDownList1.SelectedItem.Text+"<br/>");

4                 Response.Write(DropDownList1.SelectedItem.Value + "<br/>");

5                 Response.Write(DropDownList1 .SelectedValue);

6         }

頁面加載後直接點選Button1頁面出現的結果為:

選項2

1

        從結果可以看出頁面加載時選中了選項2,是以點選Button1時通過選中的項的相關屬性能得到綁定在項上的Text和Value的值。

      CheckListBox、RadioButtonList、listBox添加項ListItem的方式如上。

       下面是對CheckListBox的布置,同樣也是添加3個選項:

1                                        <asp:CheckBoxList ID="CheckBoxList1" runat="server">

2                                                 <asp:ListItem Value="0">選項1</asp:ListItem>

3                                                 <asp:ListItem Value="1">選項2</asp:ListItem>

5                                         </asp:CheckBoxList>

          在頁面中加入一個Button2,當選中選項2和3時,點選Button2在頁面中顯示出選中的選項文本,代碼如下:

1    protected void Button2_Click(object sender, EventArgs e)

3                 foreach (ListItem xm in CheckBoxList1.Items)

4                 {    

5                         if(xm.Selected)

6                                Response.Write("你選的是:"+xm.Text+"<br/>");

7                 }

8    

9                 //for (int i = 0; i < CheckBoxList1.Items.Count; i++)

10                 //{

11                 //        if (CheckBoxList1.Items[i].Selected)

12                 //                Response.Write("你選的是:" + CheckBoxList1.Items[i].Text + "<br>");

13                 //}

14         }

        上面的代碼中用了兩種循環将CheckListBox的每個項周遊出來,然後利用判斷語句判斷該項是否被選中,進而得到輸出結果。

顯示的結果為

你選的是:選項2

你選的是:選項3

        ListBox控件的選中方式中比DropDownList控件相比,支援選中多項,但是需要調整SelectionMode屬性為Single隻能選中一項,當屬性值改為Multiple将支援選中多項,并可以使用Ctrl或Shift鍵,按正常方式選擇多項,我們還是給listBox添加3個項,然後點選Button1後,在頁面中顯示使用者選擇的項文本。

1 <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple">

2                                         <asp:ListItem Value="0">選項1</asp:ListItem>

5                                         </asp:ListBox>

選中listBox項的cs代碼

1    protected void Button1_Click(object sender, EventArgs e)

3                 foreach (ListItem xm in ListBox1.Items)

4                         if (xm.Selected)

5                                 Response.Write("你選中的是"+xm.ToString()+"<br/>");

        RadioButtonList控件是将若幹個RadioButton組合在一起,控件本身保持每個選項的排他性,我們還是如一個圖一樣的設定RadioButton的三個選項,然後點選Button3,在頁面上寫出選中的單選項文本。

1 <asp:RadioButtonList ID="RadioButtonList1" runat="server">

5                                         </asp:RadioButtonList>

 點選Button3的cs代碼為:

protected void Button3_Click(object sender, EventArgs e)

        {

                Response.Write("你選的是:" + RadioButtonList1.SelectedItem.ToString()+ "<br/>");

                //Response.Write("你選的是:" + RadioButtonList1.SelectedItem.Text+ "<br/>");

}

       這兩行語句都能得到選中的單選按鈕的文本内容,

      下面請大家添加一個Button4和RadioButtonList2,然後當點選Button4時,RadioButtonList2在頁面上将自動附加元件目,内容為:初始年、第1年、第2年、……、第5年,共6個選項,并預設選中初始年。

1            protected void Button4_Click(object sender, EventArgs e)

2        {

3                ListItem li = new ListItem();

4                li.Text = "初始年";

5                RadioButtonList2.Items.Add(li);

6                RadioButtonList2.Items[0].Selected = true;

7                for (int i = 1; i <= 5; i++)

8                {

9                        li = new ListItem();

10                        li.Text = "第"+i+"年";

11                        RadioButtonList2.Items.Add(li);

12                }

13        }

       RadioButtonList2的Items集合中應加入ListItem類型的對象Li,并且利用for循環,将 RadioButtonList2控件中加入年份。

       從上面的例題我們會發現,向這些控件的項集合Items中加入一個項ListBoxItem使用Add方法,移除項還是使用Remove(項值)或RemoveAt(索引),這些用法與Winform中完全一緻,此處就不做詳解了。