C # operate the Asp.net ListBox control

Source: Internet
Author: User
<% @ Page Language = "C #" %> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <SCRIPT runat = "server"> // Add new item protected void button#click (Object sender, eventargs e) {listitem newitem = new listitem (); newitem. TEXT = This. textbox3.text; newitem. value = This. textbox4.text; this. listbox1.items. add (newitem);} // get the maximum number of sets. // The capacity of the original capacity is 8. By default, the listitem) The increase in the number will double the increase of 8 16 32 .. // However, if the capacity size is set, the protected void button2_click (Object sender, eventargs e) {response. write ("original size:" + this. listbox1.items. capacity. tostring (); this. listbox1.items. capacity = 6; response. write ("changed:" + this. listbox1.items. capacity. tostring ();} // clear all the items in the items set protected void button3_click (Object sender, eventargs e) {This. listbox1.items. clear ();} // determines whether the set contains the specified item pro Tected void button4_click (Object sender, eventargs e) {listitem item = new listitem (this. textbox1.text, this. textbox2.text); If (this. listbox1.items. contains (item) {response. write ("yes");} else {response. write ("no") ;}}// copy all items in items to protected void button5_click (Object sender, eventargs E) in the listitemcollection collection) {listitem [] items = new listitem [this. listbox1.items. count]; this. listbox1.ite Ms. copyto (items, 0); ListBox listbox2 = new ListBox (); listbox2.id = "listbox2"; listbox2.items. addrange (items); // obtain the position of listbox1, And put listbox2 after listbox1 int position = 0; foreach (control C in this. form1.controls) {position ++; If (C. id = "listbox1") {break;} This. form1.controls. addat (Position, listbox2);} // obtain the number of items in the set. Protected void button6_click (Object sender, eventargs e) {response. write (Th Is. listbox1.items. count. tostring ();} // search by text. If yes, this item is returned. If no value is found, null protected void button7_click (Object sender, eventargs E) is returned) {listitem item = This. listbox1.items. findbytext (this. textbox1.text); If (item! = NULL) {response. write (item. text + ":" + item. value) ;}} // search by value. If yes, this item is returned. If no value is found, null protected void button8_click (Object sender, eventargs e) {listitem item = This. listbox1.items. findbytext (this. textbox2.text); If (item! = NULL) {response. write (item. text + ":" + item. value) ;}} // The index protected void button9_click (Object sender, eventargs e) {listitem item = This. listbox1.items. findbytext (this. textbox1.text); If (item! = NULL) {int position = This. listbox1.items. indexof (item); response. write ("index of the queried item:" + position. tostring () ;}}// Insert the new item protected void button10_click (Object sender, eventargs e) {listitem newitem = new listitem (this. textbox1.text, this. textbox2.text); this. listbox1.items. insert (0, newitem);} // Delete the node protected void button11_click (Object sender, eventargs e) {listitem item = new listitem (this. textbox1.text, this. textbox2.text); this. listbox1.items. remove (item);} // delete protected void button12_click (Object sender, eventargs e) {int Index = This. listbox1.items. indexof (New listitem (this. textbox1.text, this. textbox2.text); this. listbox1.items. removeat (INDEX);} // The selected item protected void button13_click (Object sender, eventargs e) {This. textbox1.text = This. listbox1.selecteditem. text; this. textbox2.text = This. listbox1.selecteditem. value;} // modify protected void button14_click (Object sender, eventargs e) {This. listbox1.selecteditem. TEXT = This. textbox3.text; this. listbox1.selecteditem. value = This. textbox4.text ;} </SCRIPT> <HTML xmlns = "http://www.w3.org/1999/xhtml"> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.