Achieve level two drop-down box linkage

Source: Internet
Author: User

1. Realize the linkage of level two drop-down box.

Function: Implementation Click the Grade drop-down box to load the corresponding account drop-down box.

First step: Load the data in the Grade drop-down box first.

01. Write a method in the Gradedal layer (data access layer) to query all grade information.

/// <summary>        ///get grade information from the database/// </summary>        /// <returns>Collection of list: Grade number, Grade name</returns>         PublicList<grade>Selectgradeinfo () {List<Grade> list =NewList<grade>(); stringsql ="SELECT * from Grade"; DataTable DT=sqlhelper.executedatatable (SQL); foreach(DataRow Iteminchdt. Rows) {//an item represents a row objectGrade Grade =NewGrade (); Grade. Gradeid= Convert.ToInt32 (item["Gradeid"]); Grade. Gradename=item["Gradename"].               ToString (); List.           ADD (grade); }           returnlist; }

02. The method of invoking the data access layer at the GRADEBLL layer (the Business logic layer) is returned to the UI layer for invocation.

  Public classGRADEBLL {gradedal gd=NewGradedal (); /// <summary>        ///get grade information from the database/// </summary>        /// <returns>Collection of list: Grade number, Grade name</returns>        PublicList<grade>Selectgradeinfo () {returnGD.       Selectgradeinfo (); }        }

03. At the UI layer (presentation layer) Call the GRADEBLL layer method, receive it with the list<grade> type, bind to the drop-down box, and implement the binding of the Grade drop-down box.

//load the Grade drop-down box method, make a call in the Load event         Public voidLoadingcbograde () {//method of calling the BLL layer, receiving it with the list collectionlist<grade> list =GB.            Selectgradeinfo (); //The binding displays the value. Cbograde. DisplayMember ="Gradename"; //binds hidden values. Cbograde. ValueMember ="Gradeid"; //Binding Data SourcesCbograde. DataSource =list; }

Step two: In the Grade drop-down box, the SelectedIndexChanged (event triggered when the property value changes) loads the corresponding account information for that grade by calling the BLL layer method.

01. Write a method on the Subjectdal layer to query the grade's hidden value (that is, the grade number) according to the selected grade, and return a collection of subject objects.

  /// <summary>       ///search for subjects under this grade based on the selected grade number/// </summary>       /// <returns>collection of Account objects</returns>        PublicList<subject> Selectsubjectinfos (intID) {//Query account information based on grade number           stringsql ="Select Subjectid,subjectname from subject where [email protected]"; SqlParameter SP=NewSqlParameter ("@gradeid", id); DataTable DT=sqlhelper.executedatatable (SQL,SP); List<Subject> list =NewList<subject>(); foreach(DataRow Iteminchdt. Rows) {Subject Subject=NewSubject (); Subject. Subjectid= Convert.ToInt32 (item["Subjectid"]); Subject. Subjectname= item["Subjectname"].               ToString (); List.           ADD (subject); }           returnlist; }

02. The method of invoking the data access layer at the SUBJECTBLL layer is returned to the UI layer to invoke.

   /// <summary>       /// Search for subjects       under this grade based on the selected grade number /// </summary>       /// <returns> collection of Account objects </returns>         Public List<subject> Selectsubjectinfos (int  ID)        {            return  SD. Selectsubjectinfos (ID);        }

03. Call the SUBJECTBLL layer method in the SelectedIndexChanged (event triggered when the property value changes) in the Grade drop-down box

 //Instantiate SUBJECTBLLSUBJECTBLL SB =NewSUBJECTBLL (); //Grade drop-down box property change value triggered event        Private voidCbograde_selectedindexchanged (Objectsender, EventArgs e) {           //Get Grade number            intId=Convert.ToInt32 (Cbograde.            SelectedValue); //a method called the BLL layer is received with a collection of subject typesList<subject> list=sb.            Selectsubjectinfos (ID); //add an item all in the Account down box. List. Insert (0,NewSubject {subjectid=-1, subjectname="All" }); //empty drop-down box dataCbosubject. DataSource =NULL; //Binding Display ValuesCbosubject. DisplayMember ="Subjectname"; //binding hidden ValuesCbosubject. ValueMember ="Subjectid"; //Binding Data SourcesCbosubject. DataSource =list; }

Achieve level two drop-down box linkage

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.