<body> <form id= "Form1" runat= "Server" > <div> <asp:dropdownlist id= "DropDownList 1 "runat=" Server "appenddatabounditems=" true "autopostback=" true "></asp:DropDownList> <%--drop-down box--%> ; <%--appenddatabounditems= "True" appends a data-bound item to a statically declared list item, adding "= = = = = = = = = = =" This thing needs to change the property--%> <%--AutoPostBack means self Callback, that is, if the value of this control is changed to interact with the server, such as the DropDownList control, if set to true, when you replace the drop-down list value will refresh the page (if it is a Web page), set to Flase will not be refreshed (that is, false does not interact with the server)-- %> <asp:listbox id= "ListBox1" runat= "Server" > </asp:ListBox> <select multiple= "multiple" > <option>111</option> <option>222</option> <option>333</option> <option>44 4</option> <option>555</option> <option>666</option> <option>777</option> </select> <asp:button id= "Button1" runat= "server" text= "butt On "/> <asp:label id=" Label1 "runat=" Server "text=" Label "></asp:Label> </div> &L T;/form></body>
protected void Page_Load (object sender, EventArgs e) {Button1.Click + = button1_click; Dropdownlist1.selectedindexchanged + = dropdownlist1_selectedindexchanged; The Autopostback= property is set to "true" and triggers the event when the option is selected .
Then the Label1 is equal to the value of the option that you choose, and the following writes the assignment in the event; IsPostBack) {ListItem li = new ListItem ("= = = = = = = = = =", ""); "" Inside casually write, if do not choose directly please choose click button will appear you "" Inside Write something DropDownList1.Items.Add (LI); Add a please choose to let him in the page display, to the DropDownList1 attribute inside the AppendDataBoundItems property to true; Dropdownlist1.datasource = new Usersdata (). SelectAll (); Dropdownlist1.datatextfield = "nickname"; Dropdownlist1.datavaluefield = "Ucode"; Dropdownlist1.databind (); ListBox1.Items.Add (LI); Add a please choose to let him in the page display, to the DropDownList1 attribute inside the AppendDataBoundItems property to true; Listbox1.datasource = new Usersdata (). SelectAll (); Listbox1.datatextfield = "nickname"; Listbox1.datavaluefield = "Ucode"; Listbox1.databind (); }} void DropDownList1_SelectedIndexChanged (object sender, EventArgs e) {Label1.Text = Dropdownlist1.s Electeditem.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=" haha "/>--%> <asp:checkboxlist id=" CheckBoxList1 "runat=" Server " repeatdirection= "Horizontal" > <%-- repeatcolumns= "3" Each row shows three columns--%> <%--repeatdirection= " Horizontal "horizontal layout, as well as vertical layout, then you see from the design of the properties--%> </asp:CheckBoxList> <%--This sentence in the web opened after nothing shows, in the design, There is one unselected, right-click, edit item, add Text and 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) {//assignment, no default selected; Checkboxlist1.datasource = new Usersdata (). SelectAll (); Checkboxlist1.datatextfield = "nickname"; DataTextField Gets or sets the data source field that provides the text content for the list item Checkboxlist1.datavaluefield = "Ucode"; DataValueField Gets or sets the data source field that provides the text content for the list item if you do not write these two words, he will only provide you with a users will not give you a name, here is to give you a name (displayed name) Checkboxlist1.data Bind (); The assignment has a default check to traverse//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; Gets or sets a value that indicates whether this item is selected and the table name has been selected for these items//Checkboxlist1.items.add (LI); }}} void button1_click (object sender,EventArgs e) {//if (checkbox1.checked)//Label1.Text = Checkbox1.text; Value if (Checkboxlist1.selecteditem! = null)//if (checkboxlist1.selectedindex! =-1) cannot be empty, no error will be taken { Take a value, if you select more than one will take the first, the SelectedItem index the smallest of the//Label1.Text = CheckBoxList1.SelectedItem.Text; Gets the minimum value selected, plus. Value//takes multiple values, traversing string s = ""; foreach (ListItem li in Checkboxlist1.items)//traversal each is a ListItem object, {if (li. Selected)//selected, Gets or sets a value that indicates whether this item is selected {s + = li. Value + ","; Add equal to the following text can also be replaced by the value of the user's number;//s + = li. Text + ","; }} Label1.Text = S; } }
Asp. NET composite Control