问题:jQuery+Asp.net 联动,后台.aspx.cs 如何取到县、市的值以下default.aspx能获取数据库的值,但 string City = this.city.SelectedValue.Trim();
string Country = this.country.SelectedValue.Trim();这样取不到值,因为本人水平有限,不知如何做才能取到相关值,在此向各路高手求助,谢谢!
相关代码如下:
Default.asp.csusing System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string City = this.city.SelectedValue.Trim();
string Country = this.country.SelectedValue.Trim();
}
}
}
Default.aspx<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!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" src="Jq.js"></script> <script type="text/javascript">
function selChr(){
var pid=this.options[this.selectedIndex].value,domId='',defaultTxt='';
switch(this.id){
case 'city':domId='country';defaultTxt='==请选择县==';break;
}
if(pid)$.ajax({url:'../Common/Area/Area.ashx',type:'POST',data:{pid:pid},dataType:'json',success:function(data){ setHTML(domId,data,defaultTxt);}});
else switch(this.id){//重置选项
case 'city':setHTML('country',false,'==请选择县==');
}
}
function setHTML(Id,arr,defaultTxt){
var html='<option value="">'+defaultTxt+'</option>';
if(arr!==false)$(arr).each(function(index,item){html+='<option value="'+item.v+'">'+item.t+'</option>';});
$('#'+Id).html(html);
}
$(document).ready(function(){
$('select').not('#country').change(selChr);
$.ajax({url:'../Common/Area/Area.ashx',type:'POST',data:{pid:0},dataType:'json',success:function(data){ setHTML('city',data,'==请选择市==');}});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="请选择市县"></asp:Label>
<asp:DropDownList ID="city" runat="server"></asp:DropDownList>
<asp:DropDownList ID="country" runat="server"></asp:DropDownList>
</div>
</form>
</body>
</html>Area.ashx<%@ WebHandler Language="C#" Class="spider" %>using System;
using System.Web;
using System.Data.OleDb;
using System.Text.RegularExpressions;public class spider : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string Pid = context.Request.Form["pid"] + "", json = "";
if (!Regex.IsMatch(Pid, @"^\d+$")) Pid = "0";
OleDbConnection cn = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=" + context.Server.MapPath("#area.mdb"));
cn.Open();
OleDbDataReader dr = new OleDbCommand("select Id,AreaName from Hnfish_Area where Pid=" + Pid + " order by Id asc", cn).ExecuteReader();
while (dr.Read()) json += ",{\"v\":" + dr[0].ToString() + ",\"t\":\"" + dr[1].ToString() + "\"}";
dr.Close();
cn.Close();
if (json != "") json = json.Substring(1);
context.Response.Charset = "utf-8";
context.Response.Write("[" + json + "]");
}
public bool IsReusable {
get {
return false;
}
}}
string Country = this.country.SelectedValue.Trim();这样取不到值,因为本人水平有限,不知如何做才能取到相关值,在此向各路高手求助,谢谢!
相关代码如下:
Default.asp.csusing System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string City = this.city.SelectedValue.Trim();
string Country = this.country.SelectedValue.Trim();
}
}
}
Default.aspx<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!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" src="Jq.js"></script> <script type="text/javascript">
function selChr(){
var pid=this.options[this.selectedIndex].value,domId='',defaultTxt='';
switch(this.id){
case 'city':domId='country';defaultTxt='==请选择县==';break;
}
if(pid)$.ajax({url:'../Common/Area/Area.ashx',type:'POST',data:{pid:pid},dataType:'json',success:function(data){ setHTML(domId,data,defaultTxt);}});
else switch(this.id){//重置选项
case 'city':setHTML('country',false,'==请选择县==');
}
}
function setHTML(Id,arr,defaultTxt){
var html='<option value="">'+defaultTxt+'</option>';
if(arr!==false)$(arr).each(function(index,item){html+='<option value="'+item.v+'">'+item.t+'</option>';});
$('#'+Id).html(html);
}
$(document).ready(function(){
$('select').not('#country').change(selChr);
$.ajax({url:'../Common/Area/Area.ashx',type:'POST',data:{pid:0},dataType:'json',success:function(data){ setHTML('city',data,'==请选择市==');}});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="请选择市县"></asp:Label>
<asp:DropDownList ID="city" runat="server"></asp:DropDownList>
<asp:DropDownList ID="country" runat="server"></asp:DropDownList>
</div>
</form>
</body>
</html>Area.ashx<%@ WebHandler Language="C#" Class="spider" %>using System;
using System.Web;
using System.Data.OleDb;
using System.Text.RegularExpressions;public class spider : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string Pid = context.Request.Form["pid"] + "", json = "";
if (!Regex.IsMatch(Pid, @"^\d+$")) Pid = "0";
OleDbConnection cn = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=" + context.Server.MapPath("#area.mdb"));
cn.Open();
OleDbDataReader dr = new OleDbCommand("select Id,AreaName from Hnfish_Area where Pid=" + Pid + " order by Id asc", cn).ExecuteReader();
while (dr.Read()) json += ",{\"v\":" + dr[0].ToString() + ",\"t\":\"" + dr[1].ToString() + "\"}";
dr.Close();
cn.Close();
if (json != "") json = json.Substring(1);
context.Response.Charset = "utf-8";
context.Response.Write("[" + json + "]");
}
public bool IsReusable {
get {
return false;
}
}}
解决方案 »
- gridview response输出 excel表时 怎么把gridview的单元格提示 转成excel的单元格批注(gridview数据绑定完了)
- 帮我看一下,为什么我上传文件到虚拟服务器会找不到路径啊
- 需要一次插入多条数据,什么解决方案好?
- 高手来帮忙看看
- 我把一个用户控件放在一个DATALIST里面,现在要对用户控件动态传值,应该怎么做?请大家指点
- 安装VS.net2005以后,在VS.NET里面连接不到本机的SQL2005,但是服务已经启动了
- 请问www.gov.cn网站左边的“本网首页、今日中国、中国概况等”是个什么控件?
- 如何将一个COM组件封装成一个自定义控件?
- 菜鸟,请教asp.net中Msgbox的用法
- 请间在asp.net页面中传递参数问题?
- .net中“/”应用程序中的服务器错误
- 高手请进 调用淘宝API添加一张图片!!!
在页面加载之后你用js将city的内容加上去的。这样后台是接收不到的,你要不用参数传回去,要不将city都
加上item值
前台
<select id="quxu12" runat="server" onchange="selectcityarea('ctl00_ContentPlaceHolder1_quxu12','ctl00_ContentPlaceHolder1_quxu22','aspnetForm');">
<option selected value="0">--选择区域--</option>
</select>
<select id="quxu22" runat="server" onchange="citysun();">
<option selected value="0">--选择板块--</option>
</select> <script language="javascript">
first("ctl00_ContentPlaceHolder1_quxu12", "ctl00_ContentPlaceHolder1_quxu22", "aspnetForm", "", "");
//document.getElementById("ctl00_pageinfo_quxu1").selectedIndex =2 ;
//区域显示
$("#ctl00_ContentPlaceHolder1_quxu12").val('<%=quyu1 %>');
//selectcityarea('ctl00_ContentPlaceHolder1_quxu12', 'ctl00_ContentPlaceHolder1_quxu22', 'aspnetForm');
$("#ctl00_ContentPlaceHolder1_quxu22").val('<%=bankuai1 %>');
</script>cs文件
if(Request.Form[quxu12.UniqueID] != "0")
{
strWhere.Append(" and Area ='" + Request.Form[quxu12.UniqueID] + "' ");
}
if (Request.Form[quxu22.UniqueID] != "0")
{
strWhere.Append(" and AreaPlate='" + Request.Form[quxu22.UniqueID] + "' ");
}
你看下他的参数就知道