ASP.NET復合控件
阿新 • • 發佈:2017-08-10
sender mic rop form textfield src 交互 class 獲取
<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復合控件