ASP.NET中实现级联省份城市。求源码 就是说,两个下拉列表框,一个省份,一个城市选择省份,自动出来该省份的城市大概方法我知道,但是不想麻烦写,请高人给出源码。要省份和城市都写好的谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 前台代码: <div> 省<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> </asp:DropDownList> 市<asp:DropDownList ID="DropDownList2" runat="server"> </asp:DropDownList> </div>后台代码: protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bind(); // ShiBind(); } } public void bind()//查找省 { SqlConnection cn = new SqlConnection("server=localhost;database=student;trusted_connection=true"); SqlCommand cmd = new SqlCommand("select * from sheng",cn); DataTable dt = new DataTable(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(dt); this.DropDownList1.DataTextField = "name"; this.DropDownList1.DataValueField = "id";//获取多个表之间共同的字段 this.DropDownList1.DataSource = dt.DefaultView; this.DropDownList1.DataBind(); } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)//省级 { DataTable dt = binds(Convert.ToInt32(DropDownList1.SelectedValue)); this.DropDownList2.DataSource = dt; this.DropDownList2.DataTextField = "name"; this.DropDownList2.DataBind(); } public DataTable binds(int Id)//通过省市的ID作比较 { SqlConnection cn = new SqlConnection("server=localhost;database=student;trusted_connection=true"); string sql = string.Format("select name from shi where ShengID='{0}'",Id); SqlCommand cmd = new SqlCommand(sql,cn); DataTable dt = new DataTable(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(dt); return dt; } vs2008还是装不了************************** 求助一段JAVAServlet转换成C#代码 ImageButton控件图片显示问题 !!!!光标显示DropDownList Item的值,在线等!!!!!!!!!!! ASP客户端打开应用程序的问题 如何用c#代码关闭asp.net界面 用户控件中DataGrid的应用与显示................ repeater 中checkbox全选问题 已知一个文件的Stream, 如何通过它获得文件的大小(多少字节、多少MB等)? 高手!cookie 为什么报错? 用动软代码生成器连接数据库报错? 接口的理解。
<div>
省<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
市<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
</div>后台代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
// ShiBind();
}
}
public void bind()//查找省
{
SqlConnection cn = new SqlConnection("server=localhost;database=student;trusted_connection=true");
SqlCommand cmd = new SqlCommand("select * from sheng",cn);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
this.DropDownList1.DataTextField = "name";
this.DropDownList1.DataValueField = "id";//获取多个表之间共同的字段
this.DropDownList1.DataSource = dt.DefaultView;
this.DropDownList1.DataBind();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)//省级
{
DataTable dt = binds(Convert.ToInt32(DropDownList1.SelectedValue));
this.DropDownList2.DataSource = dt;
this.DropDownList2.DataTextField = "name";
this.DropDownList2.DataBind();
}
public DataTable binds(int Id)//通过省市的ID作比较
{
SqlConnection cn = new SqlConnection("server=localhost;database=student;trusted_connection=true");
string sql = string.Format("select name from shi where ShengID='{0}'",Id);
SqlCommand cmd = new SqlCommand(sql,cn);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
return dt;
}