標籤:click ring 清單項目 dex 控制項 opd bar 自己 drop
<body> <form id="form1" runat="server"> <div> <asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True" AutoPostBack="true"></asp:DropDownList> <%--下拉框--%> <%-- AppendDataBoundItems="True" 將資料繫結項追加到靜態聲明的清單項目上,就是加上“===請選擇===”這個東西需要改屬性--%> <%-- AutoPostBack意思是自動回傳,也就是說此控制項值更改後是否和伺服器進行互動比如Dropdownlist控制項,若設定為True,則你更換下拉式清單值時會重新整理頁面(如果是網頁的話),設定為flase就不會重新整理了(也就是false時不和伺服器互動)--%> <asp:ListBox ID="ListBox1" runat="server"> </asp:ListBox> <select multiple="multiple"> <option>111</option> <option>222</option> <option>333</option> <option>444</option> <option>555</option> <option>666</option> <option>777</option> </select> <asp:Button ID="Button1" runat="server" Text="Button" /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </div> </form></body>
protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click; DropDownList1.SelectedIndexChanged += DropDownList1_SelectedIndexChanged; // 屬性裡面設定了AutoPostBack="true",當選擇選項時候就觸發事件,
然後label1就等於你選擇的那個選項的值,下面在事件裡寫賦值; if(!IsPostBack) { ListItem li = new ListItem("===請選擇===",""); //""裡面隨便寫,如果不選擇 就直接請選擇 點擊按鈕會出現你“”裡面寫的東西 DropDownList1.Items.Add(li); //加一個請選擇要想讓他在頁面顯示,要把DropDownList1的屬性裡面的AppendDataBoundItems的屬性改為True; DropDownList1.DataSource = new UsersData().SelectAll(); DropDownList1.DataTextField = "nickname"; DropDownList1.DataValueField = "ucode"; DropDownList1.DataBind(); ListBox1.Items.Add(li); //加一個請選擇要想讓他在頁面顯示,要把DropDownList1的屬性裡面的AppendDataBoundItems的屬性改為True; ListBox1.DataSource = new UsersData().SelectAll(); ListBox1.DataTextField = "nickname"; ListBox1.DataValueField = "ucode"; ListBox1.DataBind(); } } void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { Label1.Text = DropDownList1.SelectedItem.Text; } void Button1_Click(object sender, EventArgs e) { if(DropDownList1.SelectedItem.Value != "-1") { Label1.Text = DropDownList1.SelectedValue; } }
<body> <form id="form1" runat="server"> <div> <%-- <asp:CheckBox ID="CheckBox1" runat="server" Text="哈哈" />--%> <asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatDirection="Horizontal"> <%-- RepeatColumns="3" 每行顯示三列--%> <%--RepeatDirection="Horizontal"水平布局,還有垂直布局,到時候自己從設計裡屬性裡看--%> </asp:CheckBoxList> <%--就這一句話 在網頁開啟後什麼都不顯示,在設計裡面,有一個未選定的,右鍵,編輯項,添加text和value--%> <asp:Button ID="Button1" runat="server" Text="Button" /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </div> </form></body>
protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click; if (IsPostBack==false) { //賦值,無預設選中項的; CheckBoxList1.DataSource = new UsersData().SelectAll(); CheckBoxList1.DataTextField = "nickname"; //DataTextField擷取或設定為清單項目提供常值內容的資料來源欄位 CheckBoxList1.DataValueField = "ucode"; //DataValueField擷取或設定為清單項目提供常值內容的資料來源欄位 如果不寫這兩句話 他只會為你提供一個Users 不會給你提供名字,這裡就是為了給你提供名字(顯示的名字) CheckBoxList1.DataBind(); //賦值有預設選中項的 需要遍曆 //List<Users> ulist = new UsersData().SelectAll(); //foreach (users u in ulist) //{ // listitem li = new listitem(u.nickname, u.username); // if (u.username == "xiaoyueyue" || u.username == "liuyubin") // li.selected = true; //擷取或設定一個值,該值指示是否選定此項,表名已經選定這幾項 // checkboxlist1.items.add(li); //} } } void Button1_Click(object sender, EventArgs e) { //if (CheckBox1.Checked) // Label1.Text = CheckBox1.Text; // 取值 if (CheckBoxList1.SelectedItem != null) // if (CheckBoxList1.SelectedIndex != -1) 不可為空,要不會報錯 { //取一個值,如果選擇多個會取第一個,SelectedItem索引最小的那個 // Label1.Text = CheckBoxList1.SelectedItem.Text; //擷取選中最小的值,還有.value //取多個值,遍曆 string s = ""; foreach (ListItem li in CheckBoxList1.Items) //遍曆 每一個都是ListItem對象, { if (li.Selected) //Selected,擷取或設定一個值,該值指示是否選定此項 { s += li.Value + ","; //加等於 後面的text也可以換成value就成使用者的編號; //s += li.Text + ","; } } Label1.Text = s; } }
ASP.NET複合控制項