1. Bind the collection to DropDownList during page Initialization
Copy codeThe Code is as follows:
Public void Page_Load (Object src. EventArgs e)
{
ArrayList arrValue = new ArrayList ();
ArrValue. add ("kk ");
ArrValue. add ("dd ");
ArrValue. add ("aa ");
ArrValue. add ("cc ");
// Bind the array to the DataSource attribute of the DropDownList control.
Ddl. DataSource = arrValue;
Ddl. DataBind ();
}
// Implementation
Options: <asp: DropDownList id = "ddl" runat = "server"/>
2. Add data to DropDownList during page Initialization
Copy codeThe Code is as follows:
Public void Page_Load (Object src. EventArgs e)
{
Ddl. Items. Add (new ListItem ("text", "value ");
Ddl. Items. Add (new ListItem ("text1", "value1 ");
Ddl. Items. Add (new ListItem ("text2", "value2 ");
}
// Implementation
Options: <asp: DropDownList id = "ddl" runat = "server"/>
3. dynamically bind the data read by DataReader to DropDownList
Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
If (! Page. IsPostBack)
{
String myconnstr = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + Server. MapPath (".") + ".. \ database \ db. mdb ";
OleDbConnnection myconn = new OleDbConnnection (myconnstr );
String sqlstr = "select * from test ";
OleDbCommand myComm = new OleDbCommand (sqlstr, myconn );
MyComm. Connection. Open ();
OleDbDataReader dr = myComm. ExecuteReader ();
While (dr. Read ())
{
Bj. Items. Add (new ListItem (dr ["bjmc"]. ToString (), dr ["id"]. ToString (); // Add Item
// Or bind it in this way,
// DropDownList1.Items. Add (new ListItem (myReader [1]. ToString (), myReader [0]. ToString (); // Add Item
// You must know the SQL statement or data table structure before binding.
}
Dr. Close ();
}
}
4. dynamically bind the data read by DataSet to DropDownList
Copy codeThe Code is as follows:
SqlConnection sqlconn = new SqlConnection ();
Sqlconn. ConnectionString = "workstation id = localhost; packet size = 4096; user
Id = sa; data source = db1; persist security info = False; initial catalog = DB ";
Sqlconn. Open ();
SqlDataAdapter sqldar = new SqlDataAdapter ("select UserName from forums_Users", sqlconn );
Sqldar. SelectCommand. CommandType = CommandType. Text;
DataSet Ds = new DataSet ();
Sqldar. Fill (Ds, "Users ");
Ddl. DataSource = Ds. Tables ["Users"]. DefaultView;
Ddl. DataTextField = "UsersName ";
Ddl. DataBind ();
Sqlconn. Close ();
The first line is to create an SQL connection object sqlconn;
The second line is to assign the correct value to the connection string of the newly created SQL connection object sqlconn;
The third line is to open the SQL connection object sqlconn and connect to the SQL database;
The fourth line is to create an SQL adapter object sqldar and make it use the sqlconn object to execute an SQL query statement;
The fifth line sets the command type of the sqldar object to text;
Row 6 creates a DataSet object Ds;
The seventh line is to fill in the result of sqldar execution to Ds and name it Users;
Row 8 sets the DropDownList data source to Ds Users and uses the default viewing mode;
Row 9 sets the field name UsersName corresponding to the display items in the DropDownList space;
Row 10 is the data binding method for executing DropDownList;
Row 3 closes the sqlconn object.
5. Use DataBinder. eval_r (Container. DataItem, "Table field") to output the bound data
Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
If (! Page. IsPostBack)
{
String myconnstr = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + Server. MapPath (".") + ".. \ database \ db. mdb ";
OleDbConnnection myconn = new OleDbConnnection (myconnstr );
String sqlstr = "select * from test ";
OleDbCommand myComm = new OleDbCommand (sqlstr, myconn );
MyComm. Connection. Open ();
OleDbDataReader dr = myComm. ExecuteReader ();
Ddl. DataSource = dr;
Ddl. DataBind (); bind to Inverted DD1
}
}
Directly call in DDL on the UI interface, and call the Bound Method
Copy codeThe Code is as follows:
DataBinder. eval_r (Container. DataItem, "Table field ")