ASP. NET binding with Hierarchy drop-down box (select control)

Source: Internet
Author: User

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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.