Implement two-level drop-down box linkage and two-level drop-down box Linkage

Source: Internet
Author: User

Implement two-level drop-down box linkage and two-level drop-down box Linkage

1. Implement the linkage between two levels of drop-down boxes.

Function: Click the grade drop-down box to load the drop-down box for the subject.

Step 1: First load the data in the year drop-down list.

01. Write a method on the GradeDAL layer (data access layer) to query information of all grades.

/// <Summary> /// Obtain Grade information from the database /// </summary> /// set of <returns> List: grade number, grade name </returns> public List <Grade> SelectGradeInfo () {List <Grade> list = new List <Grade> (); string SQL = "select * from Grade "; dataTable dt = SQLHelper. executeDataTable (SQL); foreach (DataRow item in dt. rows) {// an item represents a row object Grade = new grade (); Grade. gradeId = Convert. toInt32 (item ["GradeID"]); grade. gradeName = item ["GradeName"]. toString (); list. add (grade) ;}return list ;}

02. In the GradeBLL layer (business logic layer), call the data access layer method and return it to the UI Layer for calling.

Public class GradeBLL {GradeDAl gd = new GradeDAl (); /// <summary> /// Obtain Grade information from the database /// </summary> /// set of <returns> List: grade number, grade name </returns> public List <Grade> SelectGradeInfo () {return gd. selectGradeInfo ();}}

03. In the UI Layer (presentation layer), call the GradeBLL layer method and use the List <Grade> type to receive and bind it to the drop-down box to bind the Grade drop-down box.

// Load the Grade drop-down box method, call the public void Loadingcbograde () {// call The BLL Layer Method in the Load event, and use the list set to receive the List <Grade> list = gb. selectGradeInfo (); // bind the display value. Cbograde. DisplayMember = "gradename"; // bind a hidden value. Cbograde. ValueMember = "gradeid"; // bind the data source cbograde. DataSource = list ;}

Step 2: In the grade drop-down box, SelectedIndexChanged (the event triggered when the attribute value is changed) loads the corresponding subject information for this grade by calling the BLL layer method.

01. On the SubjectDAL layer, write a method to obtain the hidden value (that is, the grade number) of this grade based on the selected grade, and return a set of subject objects.

 

/// <Summary> /// query the subjects of the selected grade based on the selected grade number. // </summary> /// <returns> set of subjects </returns> public List <Subject> SelectSubjectInfos (int id) {// query subject information by grade number string SQL = "select subjectid, subjectname from subject where gradeid = @ gradeid"; SqlParameter sp = new SqlParameter ("@ gradeid", id ); dataTable dt = SQLHelper. executeDataTable (SQL, sp); List <Subject> list = new List <Subject> (); foreach (DataRow item in dt. rows) {Subject subject = new Subject (); subject. subjectId = Convert. toInt32 (item ["Subjectid"]); subject. subjectName = item ["Subjectname"]. toString (); list. add (subject);} return list ;}

02. Call the data access layer method at the SubjectBLL layer and return it to the UI Layer for calling.

/// <Summary> /// query the subjects of the selected grade based on the selected grade number. // </summary> /// <returns> set of subjects </returns> public List <Subject> SelectSubjectInfos (int id) {return sd. selectSubjectInfos (id );}

03. In the grade drop-down box, SelectedIndexChanged (the event triggered when the attribute value is changed) calls the SubjectBLL layer method.

// Instantiate SubjectBLL sb = new SubjectBLL (); // private void cbograde_SelectedIndexChanged (object sender, EventArgs e) {// obtain the grade number int id = Convert. toInt32 (cbograde. selectedValue); // call The BLL Layer Method to receive the List using the Subject type set <Subject> list = sb. selectSubjectInfos (id); // Add all items in the subaccount box. List. insert (0, new Subject {SubjectId =-1, SubjectName = "all"}); // clear the data in the drop-down box cbosubject. dataSource = null; // bind the display value cbosubject. displayMember = "subjectname"; // bind the hidden value cbosubject. valueMember = "subjectid"; // bind the data source cbosubject. dataSource = list ;}

 

Related Article

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.