Also talk about dynamic binding DropDownList

zhaozj2021-02-16  83

Also talk about dynamic binding DropDownList (2)

In my "Talking about dynamic binding DropDownList (1)", (http://blog.9cbs.net/zsxfbj/archive/

2004/07/08

/36659.ASPX) The mention is the use of Dataset as the data source to implement the DataSet Item binding. But DataSet contains too much content and structure, and we only ask for fast binding DropDownList's Item, without any operations for data. Therefore, if you do a data source in DataSet, is it a little small to use?

And when you use Dataset as a data source, we want to specify:

DropDownList1.DataTextField = "itemname"; // DropDownList's text field

DROPDOWNLIST1.DATAVALUEFIELD = "ID"; // DropDownList's field of Value

In this way, we must also know the fields of the table, this area is not very good. If we want to bind a item called Text as: all item, value 0, it will have a problem with DataSet as a data source. When I bind DROPDOWNLIST1, first specify the ITEM item I have to add above:

DropDownList1.Items.Add (New ListItem ("All Item", "0")); / / here is a 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 ();

Code of the page generated after compiling:

It seems that

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.

///

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.

///

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:

CopyRight © 2020 All Rights Reserved
Processed: 0.062, SQL: 9