Cascade issues using the drop-down menu in Asp.net

Source: Internet
Author: User

Page files

View code

<Asp: dropdownlist id = "  Dropdownlist1  " Runat = "  Server  " Onselectedindexchanged = "  Dropdownlist1_selectedindexchanged  " Autopostback = "  True  " >

<Asp: listitem> City 1 </ASP: listitem>

<Asp: listitem> City 2 </ASP: listitem>

</ASP: dropdownlist>

<Asp: dropdownlist id = " Dropdownlist2 " Runat = " Server " >

</ASP: dropdownlist>

ProgramFile

View code

Protected   Void Dropdownlist1_selectedindexchanged ( Object Sender, eventargs E)

{

If (Dropdownlist1.selecteditem. Text = " City 1 " )

{

Dropdownlist2.items. Clear (); dropdownlist2.items. Add ( " Area 1 " ); Dropdownlist2.items. Add ( " Area 2 " );

}

Else If (Dropdownlist1.selecteditem. Text = " City 2 " )

{

Dropdownlist2.items. Clear (); dropdownlist2.items. Add ( " Area 3 " ); Dropdownlist2.items. Add ( " Area 4 " );

}

}

Note that the autopostback = "true" of the first dropdownlist must be set. In fact, if such provincial/municipal linkages do not require databases, write them directly in JS, or find many existing ones,
The database is a waste of server resources, and the refreshing effect is not good.

Provides three frameworks (ASPnet, jquery, and extjs) of dropdownlist + Ajax, which are well-linked at the provincial and municipal levels)
Example: http://www.cnblogs.com/downmoon/archive/2010/06/15/1758675.html

 

Add an example of using database data:

Foreground controlsCode:

<Asp: placeholder id = "  Placeholder1 " Runat = "  Server  " >
<Asp: Label id = " Label1 " Runat = " Server " TEXT = " Module: " > </ASP: Label>
<Asp: dropdownlist id = " Dropdownlist1 " Runat = " Server "
Onselectedindexchanged = " Dropdownlist1_selectedindexchanged "
Autopostback = " True " >
</ASP: dropdownlist>
& Nbsp; <asp: Label id =" Label2 " Runat = " Server " TEXT = " Category: " Visible = " False " > </ASP: Label>
<Asp: dropdownlist id = " Dropdownlist2 " Visible = " False " Runat = " Server " >
</ASP: dropdownlist>
</ASP: placeholder>

Background code segment:

     Protected   String Sqlmodulelist = "" ;
Protected String Sqltypelist = "" ;
Sqlhelper = New Sqlhelper ();
Protected Void Page_load ( Object Sender, eventargs E)
{
If (! Ispostback)
{
Modulelist (); // Drop-down data of the first loaded Module
}
}
Protected Void Dropdownlist1_selectedindexchanged ( Object Sender, eventargs E)
{
If (Dropdownlist1.selecteditem. Text! = "" )
{
Sqltypelist = " Select typeid, typename from ky_sendmessage_type as t, ky_sendmessage_module as m
Where T. moduleid = M. moduleid and T. moduleid = " + Dropdownlist1.selectedvalue + "" ;
Dataset DS = sqlhelper. readdataset (sqltypelist );
Datatable dt = Ds. Tables [ 0 ];
If (Dt. Rows. Count! = 0 ) // Determines whether subcategories exist in the module.
{
Label2.visible = True ;
Dropdownlist2.visible = True ;
Dropdownlist2.items. Clear ();
}
Else
{
Label2.visible = False ;
Dropdownlist2.visible = False ;
}
Dropdownlist2.datasource = DT;
Dropdownlist2.datavaluefield = " Typeid " ;
Dropdownlist2.datatextfield = " Typename " ;
Dropdownlist2.databind ();
}
}
Protected Void Modulelist ()
{
Sqlmodulelist = " Select * From ky_sendmessage_module as m " ;
Dataset DS = sqlhelper. readdataset (sqlmodulelist );
Datatable dt = Ds. Tables [ 0 ];
Dropdownlist1.datasource = DT;
Dropdownlist1.datavaluefield =" Moduleid " ;
Dropdownlist1.datatextfield = " Modulename " ;
Dropdownlist1.databind ();
}

Part:

Initial loading diagram

Click to load the sub-drop-down list

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.