asp.net如何在文本框中显示 联动下拉菜单中选择的内容 就像qq中添加地址效果一样的

 我来答
381567485
2012-04-12
知道答主
回答量:15
采纳率:0%
帮助的人:10.2万
展开全部
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="reg.aspx.cs" Inherits="reg" %>

<!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>下拉菜单省市县乡村</title>
<script type="text/javascript" language="javascript">
function cityresult()
{
var sfid=document.getElementById("sf").value;

C_ajax.Getcity(sfid,result_callback);

}
function result_callback(rs)
{

if(rs.value!=null)
{
document.all("city").length=0; 
document.all("xian").length=0;
document.all("xiang").length=0;
document.all("szcz").length=0;

document.all("city").options.add(new Option("选择市",""));
document.all("xian").options.add(new Option("选择县",""));
document.all("xiang").options.add(new Option("选择乡",""));
document.all("szcz").options.add(new Option("选择村",""));
var x=rs.value;
for(var i=0;i<x.Tables[0].Rows.length;i++)
{
var name=x.Tables[0].Rows[i].cityname;
var id=x.Tables[0].Rows[i].cityid;
document.all("city").options.add(new Option(name,id));
}
}

return

}
function xianresult()
{
var cityid=document.getElementById("city").value;

C_ajax.Getxian(cityid,resultxian_callback);

}
function resultxian_callback(rs)
{

if(rs.value!=null)
{

document.all("xian").length=0;
document.all("xiang").length=0;
document.all("szcz").length=0;

document.all("xian").options.add(new Option("选择县",""));
document.all("xiang").options.add(new Option("选择乡",""));
document.all("szcz").options.add(new Option("选择村",""));
var x=rs.value;
for(var i=0;i<x.Tables[0].Rows.length;i++)
{
var name=x.Tables[0].Rows[i].xcname;
var id=x.Tables[0].Rows[i].xcid;
document.all("xian").options.add(new Option(name,id));
}
}

return

}
function xiangresult()
{
var xianid=document.getElementById("xian").value;
C_ajax.Getxiang(xianid,resultxiang_callback);
}
function resultxiang_callback(rs)
{
if(rs.value!=null)
{
document.all("xiang").length=0;
document.all("szcz").length=0;
document.all("xiang").options.add(new Option("选择乡",""));
document.all("szcz").options.add(new Option("选择村",""));
var x=rs.value;
for(var i=0;i<x.Tables[0].Rows.length;i++)
{
var name=x.Tables[0].Rows[i].xname;
var id=x.Tables[0].Rows[i].xid;
document.all("xiang").options.add(new Option(name,id));
}
}
return
}
function getczjg()
{
var xiangid=document.getElementById("xiang").value;

C_ajax.Getczm(xiangid,hdhs);
}
function hdhs(res)
{
if(res.value!=null)
{

document.all("szcz").length=0;

document.all("szcz").options.add(new Option("选择村",""));
var x=res.value;
for(var i=0;i<x.Tables[0].Rows.length;i++)
{
var name=x.Tables[0].Rows[i].cname;
var id=x.Tables[0].Rows[i].cid;
document.all("szcz").options.add(new Option(name,id));
}
}
return
}
function aa()
{
var sf=document.getElementById("sf");
var shengsy=sf.selectedIndex;
alert(sf.options[shengsy].text);

}

</script>
</head>
<body>
<form id="form1" runat="server">
<div>
省:<asp:DropDownList ID="sf" runat="server">
</asp:DropDownList>
市:<asp:DropDownList ID="city" runat="server">
</asp:DropDownList>县:<asp:DropDownList ID="xian" runat="server">
</asp:DropDownList>乡:<asp:DropDownList ID="xiang" runat="server">
</asp:DropDownList>村:
<asp:DropDownList ID="szcz" runat="server">
</asp:DropDownList>
<input id="Button1" type="button" value="button" onclick="aa();" /></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;
using AjaxPro;
public partial class reg : System.Web.UI.Page
{
C_Sf sfl = new C_Sf();
protected void Page_Load(object sender, EventArgs e)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(C_ajax));
if(!Page.IsPostBack)
{
string sql = "select * from province order by shengorderid desc";
this.sf.DataSource = sfl.dtsf(sql);
this.sf.DataTextField = "shengname";
this.sf.DataValueField = "shengid";
this.sf.DataBind();
this.sf.Attributes.Add("onclick", "cityresult();");
this.city.Attributes.Add("onclick", "xianresult();");
this.xian.Attributes.Add("onclick", "xiangresult();");
this.xiang.Attributes.Add("onclick", "getczjg();");
}
}
}
C_Sf.cs类文件中
public DataTable dtsf(string sql)
{
DataTable dt = SqlHelper.ExecuteTable(SqlHelper.ConnectionString, CommandType.Text, sql, null);
return dt;
}
在网站引用中要引用AjaxPro组件
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式