> Item1 option>
select>
The new ALL ITEM does not have. If it is behind?
/ / Specify the data source used by DROPDOWNLIST
//Dropdownlist1.items.add (New ListItem ("All Item", "0")); // New Code
DROPDOWNLIST1.DATASOURCE = dataset.tables ["Table1"]. Defaultview;
/ / Specify those fields in the table used by DROPDOWNLIST
DropDownList1.DataTextField = "itemname"; // DropDownList's text DROPDOWNLIST1.DATAVALUEFIELD = "ID"; // DropdownList Value field
DropDownList1.DATABIND ();
DropDownList1.Items.Add (New ListItem ("All Item", "0")); // New Code
The code of the compiled page is:
item5 option>
item4 option>
item3 option>
item2 option>
item1 option>
all item option>
select>
It seems that All item option> This is available, but it is on the bottom, which is not in line with our general habits. So what do you do?
Since DROPDOWNLIST1.Items can add a new listitem, and Dataset makes it too wasteful, we don't make any changes to the data, then we just read it. Let's take a look at this code:
Using system;
Using system.collections;
Using system.componentmodel;
Using system.data;
Using system.drawing;
Using system.Web;
Using system.Web.SessionState;
Using system.Web.ui;
Using system.Web.ui.webcontrols;
Using system.Web.ui.htmlcontrols;
Using system.data.sqlclient;
Using system.configuration;
Namespace binddropdownList
{
///
Summary of /// Example2.
/// summary>
Public class example2: system.web.ui.page
{
Protected system.web.ui.webcontrols.dropdownlist dropdownload1;
protected system.web.ui.webcontrols.button button1;
Private Void Page_Load (Object Sender, System.EventArgs E)
{
/ / Place the user code here to initialize the page
}
#Region Web Form Designer Generated Code
Override protected void oninit (Eventargs E)
{
//
// Codegen: This call is necessary for the ASP.NET Web Form Designer. //
InitializationComponent ();
Base.onit (E);
}
///
/// Designer supports the required method - do not use the code editor to modify
/// This method is content.
/// summary>
Private vidinitiRizeComponent ()
{
This.Button1.click = new system.eventhandler (this.button1_click);
This.Load = New System.EventHandler (this.page_load);
}
#ndregion
Private void Button1_Click (Object Sender, System.Eventargs E)
{
/ / Get the database connection string in web.config
String connString = configurationSettings.appsettings ["connectionstring"];
// Create a SqlConnection
SqlConnection
Conn
= New SQLCONNECTION (Conntring);
String SQL_SELECT = "SELECT ID, ITEMNAME" DDLITEM ORDER BY ID DESC ";
// Create a SQLCommand
Sqlcommand mycommand = new sqlcommand (SQL_SELECT,
Conn
);
// read data records and bind
mycommand.connection.open ();
// Use the DataReader to read the speed faster
SqldataReader myReader = mycommand.executeReader ();
While (MyReader.Read ())
{
DropdownList1.Items.Add (New ListItem (MyReader ["ItemName"]. TOSTRING (), MyReader ["ID"]. TOSTRING ())); // Add ITEM
// or can also be bound,
//Dropdownlist1.items.add (New ListItem (MyReader [1] .tostring (), MyReader [0] .tostring ())); // Add Item
/ / It is to be bound to be bound by knowing the SQL statement or the data table structure.
}
Mycommand.connection.close ();
}
}
}
After compiling, the effect is the same, but saves the system overhead. And we can also add special Items, such as this:
Private void Button1_Click (Object Sender, System.Eventargs E)
{
DropDownList1.Items.Add (New ListItem ("All Item", "0")); // Add an ITEM
/ / Get the database connection string in web.config
String connString = configurationSettings.appsettings ["connectionstring"];
// Create a SqlConnection
SqlConnection
Conn
= New SQLCONNECTION (Conntring);
String SQL_SELECT = "SELECT ID, ITEMNAME" DDLITEM ORDER BY ID DESC "; // Create a SQLCOMMAND
Sqlcommand mycommand = new sqlcommand (SQL_SELECT,
Conn
);
// read data records and bind
mycommand.connection.open ();
// Use the DataReader to read the speed faster
SqldataReader myReader = mycommand.executeReader ();
While (MyReader.Read ())
{
DropdownList1.Items.Add (New ListItem (MyReader ["ItemName"]. TOSTRING (), MyReader ["ID"]. TOSTRING ())); // Add ITEM
// or can also be bound,
//Dropdownlist1.items.add (New ListItem (MyReader [1] .tostring (), MyReader [0] .tostring ())); // Add Item
/ / It is to be bound to be bound by knowing the SQL statement or the data table structure.
}
Mycommand.connection.close ();
}
The compiled page code is:
all item option>
item5 option>
item4 option>
item3 option>
item2 option>
item1 option>
select>
Our purpose can be flexible.
So use SqlDataReader plus Add ListItem to bind DropDownList faster. But Dataset can also imagine this bind DropDownList:
Private void Button1_Click (Object Sender, System.Eventargs E)
{
/ / Get the database connection string in web.config
String connString = configurationSettings.appsettings ["connectionstring"];
// Create a SqlConnection
SqlConnection
Conn
= New SQLCONNECTION (Conntring);
String SQL_SELECT = "SELECT ID, ITEMNAME" DDLITEM ORDER BY ID DESC ";
// Construct a SqlDataAdapter
SqlDataAdapter myadapter = new sqldataadapter (SQL_SELECT,
Conn
);
/ / Start reading data
Cn.open ();
DataSet DataSet = New DataSet ();
Myadapter.fill (Dataset, "Table1"); conn.close ();
/ / Start binding DropDownList
DataTable DataTable = dataset.tables ["Table1"];
Foreach (DataRow DataRow In DataTable.Rows)
{
DropdownList1.Items.add (New ListItem (DataRow [1] .tostring (), DataRow [0] .tostring ()));
}
// / / Specify the data source used by DROPDOWNLIST
// //Dropdownlist1.items.add (New ListItem ("All Item", "0")); // New Code
// DropdownList1.datasource = Dataset.Tables ["Table1"]. Defaultview;
// / / Specify those fields in the table used by DROPDOWNLIST
// DropDownList1.DataTextField = "itemname"; // DropDownList's text field
// DropDownList1.DataValuefield = "ID"; // DropDownList's field of Value
// DropDownList1.DATABIND ();
// DropDownList1.Items.add (New ListItem ("All Item", "0")); // New Code
}
Of course, how to bind DropDownList is a personal preference, this is a scope of programming skills. Oh, I hope everyone can exchange programming skills and experience.
to be continued……