全國省市數據庫的引用來自網上查詢,故省略建立數據庫的過程。參考:http://www./blog/article.asp?id=28128
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="ShengShi_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www./1999/xhtml"> <head runat="server"> <title>全國省市</title> </head> <body> <form id="form1" runat="server"> <div> <asp:DropDownList ID="Pddl" runat="server" AutoPostBack="True" Height="35px" onselectedindexchanged="Pddl_SelectedIndexChanged" Width="165px"> <asp:ListItem>未選擇</asp:ListItem> </asp:DropDownList> <asp:DropDownList ID="Cddl" runat="server" Height="35px" Width="165px"> <asp:ListItem>未選擇</asp:ListItem> </asp:DropDownList> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Configuration; public partial class ShengShi_Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack)//只是初次加載頁面的時候才執(zhí)行下面代碼。 { string sqlcon = ConfigurationManager.ConnectionStrings["ProDBConn"].ConnectionString;//通過配置web.config文件,獲得連接字符串:<add name="ProDBConn" connectionString="Data Source=ZJK-PC;Initial Catalog=ProvincesDB;;User ID=sa;Password=123456" providerName="System.Data.SqlClient"/>,引用using System.Configuration; using (SqlConnection conn = new SqlConnection(sqlcon)) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "select * from promary"; using (SqlDataReader datareader = cmd.ExecuteReader()) { while (datareader.Read())//讀取數據庫的proName { string proName = datareader.GetString(datareader.GetOrdinal("proName")); Pddl.Items.Add(proName); } } } } } } private void Sqlfun(int id)//通過傳入的proID,向DropDownList添加項 { string sqlcon = ConfigurationManager.ConnectionStrings["ProDBConn"].ConnectionString; using (SqlConnection conn = new SqlConnection(sqlcon)) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "select * from city where proID=@proId order by cityID"; cmd.Parameters.Add(new SqlParameter("proId",id)); using (SqlDataReader datareader = cmd.ExecuteReader()) { while (datareader.Read()) { string proName = datareader.GetString(datareader.GetOrdinal("cityName")); Cddl.Items.Add(proName); } } } } } protected void Pddl_SelectedIndexChanged(object sender, EventArgs e) { Cddl.Items.Clear(); string proName = Pddl.SelectedItem.Text; string sqlcon = ConfigurationManager.ConnectionStrings["ProDBConn"].ConnectionString; using (SqlConnection conn = new SqlConnection(sqlcon)) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "select * from promary where proName=@proName"; cmd.Parameters.Add(new SqlParameter("proName", proName)); using (SqlDataReader datareader = cmd.ExecuteReader()) { while (datareader.Read()) { int proId = datareader.GetInt32(datareader.GetOrdinal("proID")); Sqlfun(proId); } } } } } } |
|
來自: 昵稱10504424 > 《C#》