Also talk about dynamic binding DropDownList (2)

zhaozj2021-02-16  62

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:

1 "ID =" DropdownList1 "> 5"> Item54 "> Item43> Item32"> item21> item1

The new ALL ITEM does not have.

Another: The next online game says that way, I tried it,

DropDownList1.Items.Add (New ListItem ("All Item", "0")); / / here is a new code

This sentence is changed: DropDownList1.Items.Insert (0, New ListItem ("all item", "0"));

What if you put it 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 field

DropDownList1.DataValuefield = "ID"; // DropdownList's field DROPDOWNLIST1.DATABIND ();

DropDownList1.Items.Add (New ListItem ("All Item", "0")); // New Code

The code of the compiled page is:

Item5 item4 item3 item2 item1 all item

It seems that all Item 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.

///

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:

All Item Item5 Item4 item3 item2 item1

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……

转载请注明原文地址:https://www.9cbs.com/read-17732.html

New Post(0)