Simply assign a value to the drop-down menu (DropDownList) by connecting to the database)

Source: Internet
Author: User

For your reference:
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
SqlConnection con = new SqlConnection ("server =; database = province; uid = sa; pwd = sa ;");
Con. Open ();
SqlCommand cmd = new SqlCommand ("select * from province", con );
SqlDataReader sdr = cmd. ExecuteReader ();
DropDownList1.DataSource = sdr;
DropDownList1.DataTextField = "proname ";
DropDownList1.DataValueField = "proid ";
DropDownList1.DataBind ();
Con. Close ();
Sdr. Close ();
}


}
Protected voidDropDownList1_SelectedIndexChanged(Object sender, EventArgs e)
{

SqlConnection con = new SqlConnection ("server =; database = province; uid = sa; pwd = sa ;");
Con. Open ();
SqlCommand cmd2 = new SqlCommand ("select * from city where proid =" + DropDownList1.SelectedValue, con );
SqlDataReader sdr2 = pai2.executereader ();
DropDownList2.DataSource = sdr2;
DropDownList2.DataTextField = "cityname ";
DropDownList2.DataValueField = "cityid ";
DropDownList2.DataBind ();
DropDownList2.Visible = true;
Sdr2.Close ();
Con. Close ();

}

 

 

Front-end:
<Td class = "td_page"> News Type: </td>
<Td> <asp: DropDownList ID = "dropNewType" runat = "server"> </asp: DropDownList> </td>
 
Background:
DataTable dt = CDB. FillTable ("select TypeId, TypeContent from [newsTypeTable]");
DropNewType. DataSource = dt;
DropNewType. DataTextField = "typeContent ";
DropNewType. DataValueField = "TypeId ";
DropNewType. DataBind ();

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.