页面
<!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>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script type="text/javascript" src="ajax_func.js"></script>
<script>
function changsheng(va)
{
if(va!='0')
{
var city = document.getElementById("city");
city.disabled=false;
var f=document.getElementById("qu");
f.options.length=1;
var url="Handler.ashx?type=sheng&id="+va;
send_request("GET",url,null,"text",populateClass3);
}
}
function populateClass3(){
var f=document.getElementById("city");
if(http_request.readyState==4){
if(http_request.status==200){
var list=http_request.responseText;
var classList=list.split("|");
f.options.length=1;
for(var i=0;i<classList.length;i++){
var tmp=classList[i].split(",");
f.add(new Option(tmp[1],tmp[0]));
}
}else{
alert("您所请求的页面有异常。");
}
}
}
function changshi(va)
{
if(va!='0')
{
var qu = document.getElementById("qu");
qu.disabled=false;
var url="Handler.ashx?type=shi&id="+va;
send_request("GET",url,null,"text",populateClass4);
}
}
function populateClass4(){
var f=document.getElementById("qu");
if(http_request.readyState==4){
if(http_request.status==200){
var list=http_request.responseText;
var classList=list.split("|");
f.options.length=1;
for(var i=0;i<classList.length;i++){
var tmp=classList[i].split(",");
f.add(new Option(tmp[1],tmp[0]));
}
}else{
alert("您所请求的页面有异常。");
}
}
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<FONT face="宋体"></FONT>
<!--省份列表 这里的数据刷新页面时直接去数据库得到。。-->
<select id="sheng" style="width: 121px" runat="server" onchange="changsheng(this.value)">
<option value="0">
--请选择省--
</option>
</select>
<!--城市列表 等到ajax查询出该省份的所有城市,用填充到城市列表中-->
<select id="city" runat="server" onchange="changshi(this.value)">
<option value="0">
--请选择市--
</option>
</select>
<!--区的列表 -->
<select id="qu" runat="server" >
<option value="0">
--请选择区--
</option>
</select>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</form>
</body>
</html>
后台代码
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds =SqlDataAccess.GetProvinceInfo();
string code,name;
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
code = ds.Tables[0].Rows[i]["code"].ToString();
name = ds.Tables[0].Rows[i]["name"].ToString();
this.sheng.Items.Add(new ListItem(name,code));
}
this.city.Disabled = true;
this.qu.Disabled = true;
}
protected void Button1_Click(object sender, EventArgs e)
{
int s = int.Parse(this.sheng.Value);
int c = int.Parse(this.city.Value);
int p = int.Parse(this.qu.Value);
}
Handler.ashx
<%@ WebHandler Language="C#" Class="Handler" %>using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string type = context.Request.QueryString["type"];
if (type.Equals("sheng"))
{
string id = context.Request.QueryString["id"];
context.Response.ContentType = "text/plain";
context.Response.Write(getSheng(id));//这个是从数据库中根据传来省的id 查询出来的。市的名字和主键,主键以便去查区的名字
}
else if (type.Equals("shi"))
{
string id = context.Request.QueryString["id"];
context.Response.ContentType = "text/plain";
context.Response.Write(getqu(id));
}
} public string getqu(string shi)
{
DataSet ds = SqlDataAccess.GetAreaInfo(shi);
string str = "";
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (i == ds.Tables[0].Rows.Count - 1)
{
str += ds.Tables[0].Rows[i]["code"].ToString() + "," + ds.Tables[0].Rows[i]["name"].ToString();
}
else
{
str += ds.Tables[0].Rows[i]["code"].ToString() + "," + ds.Tables[0].Rows[i]["name"].ToString() + "|";
}
}
return str.Trim();
}
public string getSheng(string sheng)
{
DataSet ds = SqlDataAccess.GetCityInfo(sheng);
string str = "";
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (i == ds.Tables[0].Rows.Count - 1)
{
str += ds.Tables[0].Rows[i]["code"].ToString() + "," + ds.Tables[0].Rows[i]["name"].ToString();
}
else
{
str += ds.Tables[0].Rows[i]["code"].ToString() + "," + ds.Tables[0].Rows[i]["name"].ToString() + "|";
}
}
return str.Trim();
}
public bool IsReusable {
get {
return false;
}
}}ajax_func.js
var http_request = false;
function send_request(method,url,content,responseType,callback){
http_request=false;
if(window.XMLHttpRequest){//Mozilla浏览器
http_request=new XMLHttpRequest();
if(http_request.overrideMimeType){
http_request.overrideMimeType("text/xml");
}
}
else{
try{
http_request=new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
http_request=new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){}
}
}
if(!http_request){
window.alert("不能创建XMLHttpRequest对象实例。");
return false;
}
if(responseType.toLowerCase()=="text"){
http_request.onreadystatechange=callback;
}else{
window.alert("响应类别参数错误。");
return false;
}
if(method.toLowerCase()=="get"){
http_request.open(method,url,true);
}
else if(method.toLowerCase()=="post"){
http_request.open(method,url,true);
http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
}else{
window.alert("http请求类别参数错误。");
return false;
}
http_request.send(content);
}
//处理返回文本格式信息的函数
function processTextResponse(){
if(http_request.readyState==4){
if(http_request.status==200){
alert("Text文档响应");
}else{
alert("您所请求的页面有异常。");
}
}
}
function processXMLResponse(){
if(http_request.readyState==4){
if(http_request.status==200){
alert("XML文档响应");
}else{
alert("您所请求的页面有异常。");
}
}
}
问题: int s = int.Parse(this.sheng.Value);
int c = int.Parse(this.city.Value);
int p = int.Parse(this.qu.Value);
c p 取的值都是0 各位高手们有什么办法啊。。 我现在只想取C的值 我想过用JS 但是 我后台代码BUTTON中获取不到啊
在线等。。
<!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>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script type="text/javascript" src="ajax_func.js"></script>
<script>
function changsheng(va)
{
if(va!='0')
{
var city = document.getElementById("city");
city.disabled=false;
var f=document.getElementById("qu");
f.options.length=1;
var url="Handler.ashx?type=sheng&id="+va;
send_request("GET",url,null,"text",populateClass3);
}
}
function populateClass3(){
var f=document.getElementById("city");
if(http_request.readyState==4){
if(http_request.status==200){
var list=http_request.responseText;
var classList=list.split("|");
f.options.length=1;
for(var i=0;i<classList.length;i++){
var tmp=classList[i].split(",");
f.add(new Option(tmp[1],tmp[0]));
}
}else{
alert("您所请求的页面有异常。");
}
}
}
function changshi(va)
{
if(va!='0')
{
var qu = document.getElementById("qu");
qu.disabled=false;
var url="Handler.ashx?type=shi&id="+va;
send_request("GET",url,null,"text",populateClass4);
}
}
function populateClass4(){
var f=document.getElementById("qu");
if(http_request.readyState==4){
if(http_request.status==200){
var list=http_request.responseText;
var classList=list.split("|");
f.options.length=1;
for(var i=0;i<classList.length;i++){
var tmp=classList[i].split(",");
f.add(new Option(tmp[1],tmp[0]));
}
}else{
alert("您所请求的页面有异常。");
}
}
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<FONT face="宋体"></FONT>
<!--省份列表 这里的数据刷新页面时直接去数据库得到。。-->
<select id="sheng" style="width: 121px" runat="server" onchange="changsheng(this.value)">
<option value="0">
--请选择省--
</option>
</select>
<!--城市列表 等到ajax查询出该省份的所有城市,用填充到城市列表中-->
<select id="city" runat="server" onchange="changshi(this.value)">
<option value="0">
--请选择市--
</option>
</select>
<!--区的列表 -->
<select id="qu" runat="server" >
<option value="0">
--请选择区--
</option>
</select>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</form>
</body>
</html>
后台代码
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds =SqlDataAccess.GetProvinceInfo();
string code,name;
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
code = ds.Tables[0].Rows[i]["code"].ToString();
name = ds.Tables[0].Rows[i]["name"].ToString();
this.sheng.Items.Add(new ListItem(name,code));
}
this.city.Disabled = true;
this.qu.Disabled = true;
}
protected void Button1_Click(object sender, EventArgs e)
{
int s = int.Parse(this.sheng.Value);
int c = int.Parse(this.city.Value);
int p = int.Parse(this.qu.Value);
}
Handler.ashx
<%@ WebHandler Language="C#" Class="Handler" %>using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string type = context.Request.QueryString["type"];
if (type.Equals("sheng"))
{
string id = context.Request.QueryString["id"];
context.Response.ContentType = "text/plain";
context.Response.Write(getSheng(id));//这个是从数据库中根据传来省的id 查询出来的。市的名字和主键,主键以便去查区的名字
}
else if (type.Equals("shi"))
{
string id = context.Request.QueryString["id"];
context.Response.ContentType = "text/plain";
context.Response.Write(getqu(id));
}
} public string getqu(string shi)
{
DataSet ds = SqlDataAccess.GetAreaInfo(shi);
string str = "";
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (i == ds.Tables[0].Rows.Count - 1)
{
str += ds.Tables[0].Rows[i]["code"].ToString() + "," + ds.Tables[0].Rows[i]["name"].ToString();
}
else
{
str += ds.Tables[0].Rows[i]["code"].ToString() + "," + ds.Tables[0].Rows[i]["name"].ToString() + "|";
}
}
return str.Trim();
}
public string getSheng(string sheng)
{
DataSet ds = SqlDataAccess.GetCityInfo(sheng);
string str = "";
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (i == ds.Tables[0].Rows.Count - 1)
{
str += ds.Tables[0].Rows[i]["code"].ToString() + "," + ds.Tables[0].Rows[i]["name"].ToString();
}
else
{
str += ds.Tables[0].Rows[i]["code"].ToString() + "," + ds.Tables[0].Rows[i]["name"].ToString() + "|";
}
}
return str.Trim();
}
public bool IsReusable {
get {
return false;
}
}}ajax_func.js
var http_request = false;
function send_request(method,url,content,responseType,callback){
http_request=false;
if(window.XMLHttpRequest){//Mozilla浏览器
http_request=new XMLHttpRequest();
if(http_request.overrideMimeType){
http_request.overrideMimeType("text/xml");
}
}
else{
try{
http_request=new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
http_request=new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){}
}
}
if(!http_request){
window.alert("不能创建XMLHttpRequest对象实例。");
return false;
}
if(responseType.toLowerCase()=="text"){
http_request.onreadystatechange=callback;
}else{
window.alert("响应类别参数错误。");
return false;
}
if(method.toLowerCase()=="get"){
http_request.open(method,url,true);
}
else if(method.toLowerCase()=="post"){
http_request.open(method,url,true);
http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
}else{
window.alert("http请求类别参数错误。");
return false;
}
http_request.send(content);
}
//处理返回文本格式信息的函数
function processTextResponse(){
if(http_request.readyState==4){
if(http_request.status==200){
alert("Text文档响应");
}else{
alert("您所请求的页面有异常。");
}
}
}
function processXMLResponse(){
if(http_request.readyState==4){
if(http_request.status==200){
alert("XML文档响应");
}else{
alert("您所请求的页面有异常。");
}
}
}
问题: int s = int.Parse(this.sheng.Value);
int c = int.Parse(this.city.Value);
int p = int.Parse(this.qu.Value);
c p 取的值都是0 各位高手们有什么办法啊。。 我现在只想取C的值 我想过用JS 但是 我后台代码BUTTON中获取不到啊
在线等。。
为什么取不到值呢
<select id="Select1" onchange="Selectcity();" name="D1" > <option>请选择省</option> </select> <select id="Select2" onchange="SelectCounty();" name="D2"> <option>请选择市</option> </select> <select id="Select3" name="D3"> <option>请选择县</option> </select>
javascript: function GetValue() { var province = document.getElementById("Select1"); var hid1 = document.getElementById('<%=HiddenField1.ClientID %>'); for(i=0;i<province.length;i++) { if(province[i].selected==true) { var value1 = province[i].innerText; hid1.value= value1; } }
后台:if (this.HiddenField1.Value != "请选择省") { model_mother.Province = this.HiddenField1.Value; }
viewState保存不了 。导致后台取不到选的 。
建议用个
<asp:HiddenField ID="df1" runat="server" />保存
<asp:HiddenField ID="df1" runat="server" />
当你的select选择之后 将这个隐藏域值 变成你select的值 。
后台就直接 df1.value就行了 。