1.
2. Data structure of tables in database
3. Front Page
<id= "pid" runat= "Server"> < value= "0" data= "|0|" > do not select parent class </option> </Select >
Note: View source code
4. Backstage Code
using System; using System.Data; using System.Web.UI.WebControls;
// binding drop-down frame with hierarchy (select control) Binddropdownlist (0"┗━""|0| ");
/// <summary> ///tie-down frame with layers/// </summary> /// <param name= "Pitemid" >Parent class Number</param> /// <param name= "Levelstr" >Hierarchy Style</param> /// <param name= "Pitemvalue" >Level value</param> protected voidBinddropdownlist (intPitemid,stringLEVELSTR,stringpitemvalue) { //get a list of news categoriesDataTable dt = Express.BLL.NewsCategory.GetDataSet (). tables[0]; //determine if it is a parent class, change its level value if(Pitemid! =0) {Levelstr+=string. IsNullOrEmpty (LEVELSTR)?"┗━":"┻━"; Pitemvalue + = Pitemid +"|"; } //to iterate over a data item in a control's list control foreach(DataRow DrinchDt. Select ("parentid="+Pitemid)) { //initializing data items in a list controlListItem Li =NewListItem (); //set the text displayed for a data itemLi. Text = Levelstr + dr["ItemName"]. ToString (); //set the value associated with a data itemLi. Value = dr["Id"]. ToString (); //to add Data property values for an itemLi. Attributes.Add ("Data", Pitemvalue + li. Value +"|"); //append a data item to the control This. PID. Items.Add (LI); //traversing a subclass data item in a list control binddropdownlist (Convert.ToInt32 (li. Value), Levelstr, pitemvalue); } }
5. Database part
/// <summary> ///get all categories for which the name is not empty/// </summary> /// <returns></returns> PublicDataSet GetDataSet () {stringsql ="SELECT * from newscategory where itemname<> "ORDER by replace (Itemvalue,convert (varchar,id) + ' | ', '), Sortvalue"; returnSqlHelper.ExecuteDataset (connstring.connreadonly, CommandType.Text, SQL,NULL); }
Points of knowledge involved:
(1) binding data in a database to a drop-down box
<id= "pid" runat= "Server"> < value= "0" data= "|0|" > do not select parent class </option></select>
Method One:
DataSet ds = Express.BLL.AdminLeft.GetDataSet (); foreach in DS. tables[0]. Rows) { pid. Items.Add (newListItem (dr["ItemName"). ToString (), dr["Id"]. ToString ())); }
Note:
ListItem reference space: System.Web.UI.WebControls;
ListItem (string text,string value); /text refers to the literal displayed in ListItem; value refers to ListItem.
Method Two:
Pid. DataSource = DT; // to set the data source for a control " ItemName " // Set the text displayed for a data item " Id "; // Set the value associated with a data item Pid. DataBind (); // to bind data to a control
(2) "DataTable.Select method" return value: Array of DataRow objects
Dt. Select (filter condition);
Dt. Select ();
Dt. Select (filter criteria, sort);