Also talk about binding DropDownList (3)

xiaoxiao2021-03-06  183

In the previous article, I talked some ways. If you carefully experience it, it is still an old way, it does not reflect the idea of ​​object-oriented process programming. And the method of the two articles in front of the previous article is not high.

We can give the DDLITEM table to the object, regard the fields in the table as its attribute, I first create an object class, class named DDLITEMINFO, the code as follows:

Using system;

Namespace binddropdownList

{

///

/// mainly of the DDLITEM table objectification,

// / turn the field of the table to attribute, can be arbitrary

/// Get or set the value of this property.

///

[Serializable]

Public Class DDLITEMINFO

{

/ / Define internal variables

PRIVATE INT_ID;

Private string _itemname;

/ / Define 2 Reconstruction Function

Public ddliteminfo () {}

Public DDLITEMINFO (Int ID, String ItemName)

{

_id = id;

_Itemname = itemname;

}

/ / Define the method of the member

Public Int ID

{

get

{

Return_ID;

}

set

{

_id = value;

}

}

Public String ItemName

{

get

{

Return_itemname;

}

set

{

_Itemname = value;

}

}

}

}

Then create the class library of the method used by the table, class name DDLITEM.CS, the code is as follows:

Using system;

Using system.collections;

Using system.data;

Using system.data.sqlclient;

Namespace binddropdownList

{

///

/// The various SQL operations for the DDLITEM table.

///

Public Class DDLITEM

{

Public ddlitem () {}

/ / Define Database Connection Strings

Private const string SQL_CONN_STRING = system.configuration.configurationSettings.AppSettings ["Connectionstring"];

/ / Define SQL statements

Private const string SQL_SELECT_DDLIEMS = "SELECT ID, ITEMNAME" DDLITEM ORDER BY ID DESC ";

/ / Construct a method to read records in all DDLITEM tables

Public IList Get_DDLITEMS ()

{

/ / Installation A number of array of dynamically increased lengths

IList itemlist = new arraylist ();

/ / Define database connections

SqlConnection myconn = new sqlconnection (SQL_CONN_STRING);

// Define SQL commands

Sqlcommand mycommand = new sqlcommand (SQL_SELECT_DDLIEMS, MyConn);

// Open the database

mycommand.connection.open ();

/ / Define a SqldataReader

SqlDataReader rdr = mycommand.executeReader ();

/ / Start loop reading record

While (Rdr.Read ()) {

/ / Construct an instantiated DDLITEM table object

DDLITEMINFO ITEMINFO = New DDLITEMINFO

Rdr.isdbnull (0)? 0: Rdr.Getint32 (0),

Rdr.isdbnull (1)? String.empty: rdr.getstring (1)

);

ItemList.Add (iteminfo);

}

// Turn off SqlDataReader and SqlConnection

Rdr.close ();

Mycommand.connection.close ();

Return ItemList;

}

}

}

The method we have to operate is defined. How to call the problem, then create an ASPX page, the 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;

Namespace binddropdownList

{

///

Summary of /// Example3.

///

Public class example3: 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 records using the DDLITEM class's GET_DDLITEMS method

IList List = New DDLITEM (). Get_ddlitems ();

/ / Decades if there is any record

IF (List.count! = 0)

{

// Add the record to DropDownList1

For (int i = 0; i

{

DDLITEMINFO ITEMINFO = (DDLITEMINFO) List [i];

DropDownList1.items.Add (New ListItem (iteminfo.id);

}

}

}

}

}

This way, when we manage DDLITEM content, we can directly call GET_DDLITEMS. You can list all DDLITEM information, and This also improves efficiency.

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

New Post(0)