Also talk about dynamic binding DropDownList (1)
Said, a lot of DropDownList options are not fixed, which is dynamically changed, one method is to die on the page, change the page directly, can be modified directly. But many people use dynamic binding, so DROPDOWNLIST's text and value are dynamically generated.
First let's create a menu option to store DropDownList:
Create Table DDLITEM - drop-down menu selection table
(
ID Int IDENTITY PRIMARY Key, - Number
ItemName Varchar (20) - Menu Name
)
Add it in Web.Config:
Appsettings>
One of the most common methods is to bind the value of DROPDOWNLIST with DataSet as a data source.
First we first add a DropDownList and a Button on the page, as shown in the figure:
The source code is as follows:
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 /// Example1.
///
Public class example1: 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.
///
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 ";
// 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
/ / Specify the data source used by DROPDOWNLIST
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 ();
}
}
}
The effect is as follows:
Unbound
After the binding
This method is relatively simple, you can flexibly specify any field of any field in a table for the TEXT and VALUE values of DROPDOWNLIST. But the DataSet contains too much attribute, which is equivalent to a offline database. So, don't agree with this method to bind DropDownList.