<body> <form id="Form1"runat="Server"> <div> <asp:dropdownlist id="DropDownList1"runat="Server"appenddatabounditems="True"autopostback="true"></asp:DropDownList> <%--drop-down box--%> <%--appenddatabounditems="True"Append the data-bound item to the list item of the static declaration, adding "= = = = = = = = = = =" This thing needs to change the property--%> <%--AutoPostBack means automatic callback, that is, if this control value changes after the server interaction such as Dropdownli St 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 refresh (that is, false when not interacting with the server)--%> <asp:listbox id="ListBox1"runat="Server"> </asp:ListBox> <Selectmultiple="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 voidPage_Load (Objectsender, 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; if(!IsPostBack) {ListItem Li=NewListItem ("= = = Please select = =","");//" " to write, if you do not select directly please select the Click button will appear in your ""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 =NewUsersdata (). 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 =NewUsersdata (). SelectAll (); Listbox1.datatextfield="Nickname"; Listbox1.datavaluefield="Ucode"; Listbox1.databind (); } } voidDropDownList1_SelectedIndexChanged (Objectsender, EventArgs e) {Label1.Text=DropDownList1.SelectedItem.Text; } voidButton1_Click (Objectsender, 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 of--%> <%--repeatdirection="Horizontal"Horizontal layout, as well as vertical layout, and then I see from the design of the properties--%> </asp:CheckBoxList> <%--This sentence in the Web page after the opening of nothing, in the design, there is a unselected, right Keys, edit items, 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 voidPage_Load (Objectsender, EventArgs e) {Button1.Click+=button1_click; if(ispostback==false) { //assignment, with no default selection;Checkboxlist1.datasource =NewUsersdata (). SelectAll (); Checkboxlist1.datatextfield="Nickname";//DataTextField Gets or sets the data source field that provides the text content for the list itemCheckboxlist1.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.databind (); //assignment has the need to traverse the default selected item//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 is already selected//Checkboxlist1.items.add (LI); //} } } voidButton1_Click (Objectsender, EventArgs e) { //if (checkbox1.checked)//Label1.Text = Checkbox1.text; //Take value if(Checkboxlist1.selecteditem! =NULL)//if (checkboxlist1.selectedindex! =-1) cannot be empty, do not error { //take a value, if you select more than one will take the first, the SelectedItem index the smallest one//Label1.Text = CheckBoxList1.SelectedItem.Text; //Gets the minimum value selected, plus the. Value//take multiple values, traverse strings =""; foreach(ListItem LiinchCheckboxlist1.items)//traversing each one is a ListItem object, { if(Li. Selected)//Selected, Gets or sets a value that indicates whether this item is selected{s+ = li. Value +",";//plus equals the text in the back can also be replaced by the value of the user's number;//s + = li. Text + ",";}} Label1.Text=s; } }
Asp. NET composite Control