DropDownList控件與ListBox控件最終將會(huì)被解析成為代碼< select >< /select >
DropDownList控件的定義:
<asp:DropDownList id="DropDownList1" runat="server"
DataSource="<% databindingexpression %>"
DataTextField="DataSourceField"
DataValueField="DataSourceField"
AutoPostBack="True|False"
OnSelectedIndexChanged="OnSelectedIndexChangedMethod" />
<asp:Listitem value="value" selected="True|False">
Text
</asp:Listitem>
</asp:DropDownList>
DataSource、DataTextField、DataValueField這三個(gè)屬性的應(yīng)用需要涉及DataBind的知識(shí)AutoPostBack屬性:表示當(dāng)DropDownList控件的所選項(xiàng)發(fā)生改變后,自動(dòng)提交當(dāng)前頁面OnSeletedIndexChanged事件在DropDownList控件所選項(xiàng)發(fā)生變化后發(fā)生。
DropDownList控件的SelectedItem對(duì)象表示在執(zhí)行過程中被選中的Item項(xiàng)、Item項(xiàng)擁有三個(gè)屬性。其一為Text,其二為Value它們分別表示Item項(xiàng)所顯示的文字與所表示的值,如果Value屬性沒有設(shè)置,則默認(rèn)Value屬性值等Text屬性,第三個(gè)屬性Selected則用于判斷某Item項(xiàng)是否被選中添加DorpDownList控件的Item子項(xiàng)。
添加DorpDownList控件的Item子項(xiàng)的方法有三種:
第一種:使用<asp:ListItem>方法
<asp:DropDownList id="weste" runat="server">
<asp:ListItem Value="0">小學(xué)</asp:ListItem>
<asp:ListItem Value="1">中學(xué)</asp:ListItem>
<asp:ListItem Value="2">大學(xué)</asp:ListItem>
</asp:DropDownList>
第二種:使用Items屬性的Add方法1
DropDownList.Items.Add(ItemText)
Items屬性表示DropDownList控件所有Item項(xiàng)的集合,Add操作既是在這個(gè)集合中插入新的Item項(xiàng)。第二種方法用于只設(shè)定Item項(xiàng)的Text屬性(實(shí)際上也指定了Value屬性,此時(shí)Value屬性值等于Text屬性值)。
第三種:使用Items屬性的Add方法2
DropDownListItems.Add(new ListItem)ItemText,ItemValue))
第三種方法同時(shí)設(shè)定了Item項(xiàng)的Text屬性與Value屬性,它是通過添加一個(gè)ListItem類來實(shí)現(xiàn)的。ListItem類使用了兩個(gè)參數(shù),第一個(gè)參數(shù)表示Text屬性值,第二個(gè)參數(shù)表示Value屬性值。