Common data binding for Listbox,checkboxlist,dropdownlist,radiobuttonlist

Source: Internet
Author: User

Common usage of listbox,checkboxlist,dropdownlist,radiobuttonlist four is the selection of the control, the use of a similar, basic is to specify the key value pairs: Directly add the selection:
 voidway1 () {DROPDOWNLIST1.ITEMS.ADD ("C #"); DROPDOWNLIST1.ITEMS.ADD ("vb"); DROPDOWNLIST1.ITEMS.ADD ("C"); DROPDOWNLIST1.ITEMS.ADD ("Java"); LISTBOX1.ITEMS.ADD ("C #"); LISTBOX1.ITEMS.ADD ("VB"); LISTBOX1.ITEMS.ADD ("C + +"); LISTBOX1.ITEMS.ADD ("JAVA"); CHECKBOXLIST1.ITEMS.ADD ("C #"); CHECKBOXLIST1.ITEMS.ADD ("VB"); CHECKBOXLIST1.ITEMS.ADD ("JavaScript"); CHECKBOXLIST1.ITEMS.ADD ("PHP"); RADIOBUTTONLIST1.ITEMS.ADD ("ASP"); RADIOBUTTONLIST1.ITEMS.ADD ("PHP"); RADIOBUTTONLIST1.ITEMS.ADD ("JSP"); RADIOBUTTONLIST1.ITEMS.ADD ("Ajax"); } Haas Table method:voidWay2 () {Hashtable HT=NewHashtable (); Ht. ADD ("1","C #"); Ht. ADD ("2","C + +"); Ht. ADD ("3","PHP"); Ht. ADD ("4","CGI");  This. Dropdownlist2.datasource=HT; Dropdownlist2.datatextfield="Value"; Dropdownlist2.datavaluefield="Key";    Dropdownlist2.databind ();  This. Listbox2.datasource=HT; Listbox2.datatextfield="value"; Listbox2.datavaluefield="Key";    Listbox2.databind ();  This. Checkboxlist2.datasource=HT; Checkboxlist2.datatextfield="value"; Checkboxlist2.datavaluefield="Key";    Checkboxlist2.databind ();  This. Radiobuttonlist2.datasource=HT; Radiobuttonlist2.datatextfield="value"; Radiobuttonlist2.datavaluefield="Key";   Radiobuttonlist2.databind (); }sortedlist Method:voidway3 () {SortedList S1=NewSortedList (); S1. ADD ("1","haha"); S1. ADD ("2","hehe"); S1. ADD ("3","Xixi"); S1. ADD ("4","Heihei");  This. Dropdownlist3.datasource=S1; Dropdownlist3.datatextfield="value"; Dropdownlist3.datavaluefield="Key";   Dropdownlist3.databind (); Use the Data Object Dataread method:voidWay4 () {OleDbConnection cn=NewOleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0;Data source="+server.mappath ("Sailing.mdb")+"; Persist Security Info=false"); cn.    Open (); OleDbCommand cmd=NewOleDbCommand (); Cmd. Connection=cn; Cmd.commandtext="Select Rolename,roleid from Role"; OleDbDataReader Dr=cmd.    ExecuteReader ();  while(Dr. Read ()) {ListItem Li=NewListItem (dr[0]. ToString (), dr[1].     ToString ());  This.    ListBox4.Items.Add (LI); } Dr.    Close (); cn.   Close (); } with Data set method: OleDbConnection cn=NewOleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0;Data source="+server.mappath ("Sailing.mdb")+"; Persist Security Info=false"); cn.     Open (); OleDbCommand cmd=NewOleDbCommand (); Cmd. Connection=cn; Cmd.commandtext="Select Rolename,roleid from Role"; OleDbDataAdapter ADP=NewOleDbDataAdapter (CMD); DataSet DS=NewDataSet (); Adp.     Fill (DS);  This. Checkboxlist4.datasource=ds. tables[0];  This. Checkboxlist4.datatextfield=ds. tables[0]. columns[0].     ToString ();  This. Checkboxlist4.datavaluefield=ds. tables[0]. columns[1].     ToString ();  This. Checkboxlist4.databind (); There is also an array method:<%@ Page language="C #"autoeventwireup="True"%><script language="C #"runat="Server">voidPage_Load (Object Sender,eventargs e) {ArrayList array=NewArrayList (); array. ADD ("Windows"); array. ADD ("Linux"); array. ADD ("Unix"); Checkbox_1.datasource=Array; Checkbox_1.databind (); Radiolist_1.datasource=Array; Radiolist_1.databind ();}</script><form id="form_1"runat="Server"><asp:checkboxlist id="checkbox_1"runat="Server"/><input id="button_1"Type="Button"Value="Checkbutton"runat="Server"/><asp:radiobuttonlist id="radiolist_1"runat="Server"/><input id="button_2"Type="Button"Value="RadioButton"runat="Server"/></form>

Common data binding for Listbox,checkboxlist,dropdownlist,radiobuttonlist

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.