Two Methods for binding DropdownList: bind dropdownlist
Dynamic binding method 1: dynamically bind fields in the database.
SqlConnection conn = UtilitySqlClass. operateDataBase. returnConn (); string strSQL = "select * from CompanyType"; SqlDataAdapter ada = new SqlDataAdapter (strSQL, conn); DataSet ds = new DataSet (); ada. fill (ds, "CompanyType"); DropDownList1.DataSource = ds. tables ["CompanyType"]. defaultView; DropDownList1.DataValueField = ds. tables ["CompanyType"]. columns [1]. columnName; DropDownList1.DataTextField = ds. tables ["CompanyType"]. columns [1]. columnName; DropDownList1.DataBind (); ds. dispose ();
Dynamic binding method 2: Use the DropDownList. Items. Add method.
Protected void Page_Load (object sender, EventArgs e) {if (! IsPostBack) {SqlConnection conn = UtilitySqlClass. operateDataBase. returnConn (); try {conn. open (); this. dropDownList1.Items. add (""); string strSQL = "select CompanyType from CompanyType"; SqlCommand com = new SqlCommand (strSQL, conn); SqlDataReader dr = com. executeReader (); while (dr. read () {this. dropDownList1.Items. add (dr ["CompanyType"]. toString () ;}} catch (Exception ex) {Response. write ("<scirpt> alert ('" + ex. message. toString () + "') </script>");} finally {conn. close ();}}}
Method 1: string ConnString = ConfigurationSettings. etettings ["ConnectionString"]; // create a SqlConnection Conn = new SqlConnection (ConnString );
String SQL _Select = "select id, ItemName from DDLItem order by id desc ";
// Construct a SqlDataAdapter
SqlDataAdapter myAdapter = new SqlDataAdapter (SQL _Select, Conn );
// Start reading data
Conn. Open ();
DataSet dataSet = new DataSet ();
MyAdapter. Fill (dataSet, "Table1 ");
Conn. Close ();
// Start binding DropDownList
// Specify the data source used by DropDownList
DropDownList1.DataSource = dataSet. Tables ["Table1"]. DefaultView;
// Specify the fields in the table used by DropDownList
DropDownList1.DataTextField = "ItemName"; // The Text field of dropdownlist
DropDownList1.DataValueField = "id"; // The Value Field of dropdownlist
DropDownList1.DataBind ();
Method 2:
Con. Open ();
SqlCommand cmd = new SqlCommand (strSql, con );
SqlDataReader dr = cmd. ExecuteReader ();
While (dr. Read ())
{DropDownList1.Items. Add (new ListItem (dr ["status"]. ToString (), dr ["status_Id"]. ToString ()));}
How can DropDownList be bound to data in the database?
Step 1: connect to the database
Step 2: obtain data/
Step 3: specify a data source
Step 4: Specify the text and value values.
Step 5: bind
Reference code:
SqlConnection con3 = new SqlConnection ("Server =.; user id = sa; password = 123456; Database = FABS ");
Con3.Open ();
SqlCommand cmd3 = new SqlCommand ();
Optional 3.commandtext = "select * from sheng ";
Optional 3.commandtype = CommandType. Text;
Listen 3.connection = con3;
SqlDataReader sdr3 = ipv3.executereader ();
DropDownList1.DataSource = sdr3;
DropDownList1.DataTextField = "shengming ";
DropDownList1.DataBind ();
Con3.Close ();
How to bind data between two dropdownlists
Code:
<Asp: DropDownList ID = "bid" runat = "server"> </asp: DropDownList>
Code in CS
String cStr = "select id, classname from classify order by id asc ";
SqlDataAdapter cRs = new SqlDataAdapter (cStr, conn );
Conn. Open ();
DataSet cRd = new DataSet ();
CRs. Fill (cRd );
Bid. DataSource = cRd;
Bid. DataTextField = "classname"; // bind the display name field
Bid. DataValueField = "id"; // field of the bound value
Bid. DataBind ();
Conn. Close ();
I have two methods here. You can choose one.
Www.w17x.com/AritcleDisplay.aspx? Id = 256
Www.w17x.com/AritcleDisplay.aspx? Id = 557