<% @ Page Language = "C #" autoeventwireup = "true" codefile = "ListBox. aspx. cs" inherits = "test_listbox" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: ListBox id = "listbox1" runat = "server" autopostback = "true" Height = "129px" onselectedindexchanged = "listbox#selectedindexchanged"
Selectionmode = "multiple" width = "103px">
<Asp: listitem> Apple </ASP: listitem>
<Asp: listitem> persimmon </ASP: listitem>
</ASP: ListBox> <br/>
<Br/>
<Asp: textbox id = "textbox1" runat = "server"> </ASP: textbox> <br/>
<Br/>
<Asp: button id = "button1" runat = "server" onclick = "button#click" text = "add"/>
<Br/>
<Asp: Label id = "label1" runat = "server" text = "label"> </ASP: Label> <br/>
<Br/>
<Br/>
====================< Br/>
<Asp: Label id = "label2" runat = "server" text = "Province" width = "26px"> </ASP: Label>
<Asp: dropdownlist id = "dropdownlist1" runat = "server" autopostback = "true" onselectedindexchanged = "dropdownlist1_selectedindexchanged">
</ASP: dropdownlist> <br/>
<Asp: Label id = "label3" runat = "server" text = "city" width = "27px"> </ASP: Label> <asp: dropdownlist
Id = "dropdownlist2" runat = "server">
</ASP: dropdownlist> <br/>
<Br/>
<Br/>
<Br/>
<Br/>
<Br/>
<Br/>
<Br/>
<Br/>
</Div>
</Form>
</Body>
</Html>
========================================================== ====================
Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Data. sqlclient;
Public partial class test_listbox: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (! This. ispostback)
{
Sqlconnection con = db2.createconnection ();
Con. open ();
Sqlcommand cmd = new sqlcommand ("select * From Province", con );
Sqldatareader SDR = cmd. executereader ();
This. dropdownlist1.datasource = SDR;
This. dropdownlist1.datatextfield = "proname ";
This. dropdownlist1.datavaluefield = "proid ";
This. dropdownlist1.databind ();
SDR. Close ();
// Con. Close ();
Sqlcommand cmdcity = new sqlcommand ("select * from city where proid =" + this. dropdownlist1.selectedvalue. tostring (), con );
SDR = cmdcity. executereader ();
This. dropdownlist2.datasource = SDR;
This. dropdownlist2.datavaluefield = "cityid ";
This. dropdownlist2.datatextfield = "cityname ";
This. dropdownlist2.databind ();
SDR. Close ();
Con. Close ();
}
}
Protected void button#click (Object sender, eventargs E)
{
This. listbox1.items. Add (this. textbox1.text );
}
Protected void listbox1_selectedindexchanged (Object sender, eventargs E)
{
Label1.text = "you have selected:" + this. listbox1.selecteditem. text;
}
Protected void dropdownlistincluselectedindexchanged (Object sender, eventargs E)
{
Sqlconnection con = db2.createconnection ();
Con. open ();
Sqlcommand cmd = new sqlcommand ("select * from city where proid =" + this. dropdownlist1.selectedvalue. tostring (), con );
Sqldatareader SDR = cmd. executereader ();
This. dropdownlist2.datasource = SDR;
This. dropdownlist2.datavaluefield = "cityid ";
This. dropdownlist2.datatextfield = "cityname ";
This. dropdownlist2.databind ();
SDR. Close ();
Con. Close ();
}
}
The following is a link between dropdownlist and ListBox!