Use of the DropDownList control (data binding) (. NET Learning Note II) (2006-10-12-07:28:49)
Reproduced
|
Category:. NET learning Notes |
First, bind the collection to DropDownList when the page is initialized
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 an array to the DataSource property of the DropDownList control
Ddl. DataSource = Arrvalue;
Ddl. DataBind ();
}
Realize
The options are: <asp:dropdownlist id= "DDL" runat= "Server"/>
Second, add the data public void Page_Load (Object src) to DropDownList when the page is initialized. EventArgs e)
{
Ddl. Items.Add (New ListItem ("text", "value");
Ddl. Items.Add (New ListItem ("Text1", "value1");
Ddl. Items.Add (New ListItem ("Text2", "value2");
}
Realize
The options are: <asp:dropdownlist id= "DDL" runat= "Server"/>
Third, dynamically bind the data read DataReader to the DropDownList
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 it can be bound,
DROPDOWNLIST1.ITEMS.ADD (New ListItem (myreader[1). ToString (), myreader[0]. ToString ()));//Add Item
is to be bound in such a way as to know the SQL statement or the structure of the data table.
}
Dr. Close ();
}
}
Iv. dynamically bind the data read by the dataset to the DropDownList
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 a SQL connection object sqlconn;
The second line assigns the correct value to the connection string for the new SQL connection object sqlconn;
The third line is to let the SQL Connection object Sqlconn Open, connect the SQL database;
Line four is to create a SQL adapter object Sqldar, and let it use the Sqlconn object to execute an SQL query statement;
Line Five is the text type of the command type that sets the Sqldar object;
Line six is to create a DataSet object ds;
The seventh line is to populate the DS with the results of sqldar execution and will be named users;
The eighth line is to set the DropDownList data source to the users of DS and use the default viewing mode;
The Nineth line is the field name Usersname that sets the display item of the DropDownList space;
The tenth line is the data binding method of executing DropDownList;
The other line is to close the Sqlconn object.
V. Output bound data using Databinder.eval_r (Container.DataItem, table field) 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 (); Binding in inverted DD1
}
}
Called directly in the DDL of the UI interface, the bound method call
Databinder.eval_r (Container.DataItem, "table field")
(GO). NET control DropDownList dynamically bound data