Default页面前台代码如下:
<script type="text/javascript">
//声明一个全局变量,用来保存XMLHttpRequest对象的引用(未初始化)
var xmlHttp = false;
//创建XMLHttpRequest对象函数
function GetXMLHttpRequest()
{
// 由于 XMLHttpRequest 对象在各个浏览器中实现机制不同,所以要综合考虑
var xmlHttp = false;
//如果是Mozilla浏览器
if (!xmlHttp && typeof XMLHttpRequest != 'object') //undefined
{
xmlHttp = new XMLHttpRequest();
}
// if(window.XMLHttpRequest)
// {
// xmlHttp = new XMLHttpRequest();
// if(xmlHttp.overrideMimeType)
// {
// xmlHttp.overrideMimeType("text/xml");
// }
// }
//如果是IE浏览器
else if(window.ActiveXObject)
{
try
{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e2)
{
xmlHttp = false;
}
}
}
return xmlHttp;
}
function getSecondCategory()
{
var RootCategory = document.getElementById("RootCategory");
//if (RootCategory.Items == null)
//return;
alert("xxx");
xmlHttp = GetXMLHttpRequest();
// 构造将XMLHttpRequest对象将请求的服务器端的URL字符串
var url = "GetSecondCategory.aspx?CategoryID=" + RootCategory.value;
// 建立与服务器端的连接
xmlHttp.open("GET", url, true);
// 设置回调函数
xmlHttp.onreadystatechange = callBack_getSecondCategory;
// 发送请求
xmlHttp.send(null);
}
function callBack_getSecondCategory()
{
var lbl=document.getElementById("lbl");
if (4 == xmlHttp.readyState)
{
alert("4");
if(200 == xmlHttp.status)
{
alert("200");
//通过responseText属性得到服务器端实际返回的文本
lbl.innerHTML =xmlHttp.responseText;
}
}
else
{
lbl.innerHTML =xmlHttp.readyState;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<select id="RootCategory" style="width: 121px" runat="server" onchange="return getSecondCategory();">
<option value="0">--请选择分类--</option>
</select>
<!--二级分类列表 等到ajax查询出该大类的所有子分类,用填充到分类列表中-->
<select id="SecondCategory" runat="server">
<option value="0">--请选择分类--</option>
</select>
<!--三级分类列表 -->
<select id="ThirdCategory" runat="server" >
<option value="0">--请选择分类--</option>
</select>
<span id="lbl"></span>
</div>
</form>
</body>Default页面后台代码如下:
protected void Page_Load(object sender, EventArgs e)
{
BindRootCategory();
} protected void BindRootCategory()
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connstr"].ToString());
SqlCommand cmd = new SqlCommand("select CategoryName,CategoryID from Category where ParentCategoryID=0", con);
try
{
con.Open();
RootCategory.DataSource = cmd.ExecuteReader();
RootCategory.DataTextField = "CategoryName";
RootCategory.DataValueField = "CategoryID";
RootCategory.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
con.Close();
}
}服务器处理页面GetSecondCategory.aspx.cs代码
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
//System.Threading.Thread.Sleep(1000);
String SecondCategory = String.Empty;
SecondCategory = getSecondCategory(); // 发送验证结果
// Response.Clear();
// Response.ContentType = "text/xml";
Response.ContentType = "text/html";
Response.Write(SecondCategory.ToString());
Response.Flush();
Response.Close();
} protected String getSecondCategory()
{
String CategoryName=String.Empty;
using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connstr"].ToString()))
{
SqlCommand cmd = new SqlCommand("select CategoryID,CategoryName from Category where ParentCategoryID=@CategoryID", con);
cmd.Parameters.AddWithValue("@CategoryID", 1);
try
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
CategoryName += reader[1].ToString() + ",";
}
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
return CategoryName;
}
为什么提示服务器端没有返回响应?谢谢!
<script type="text/javascript">
//声明一个全局变量,用来保存XMLHttpRequest对象的引用(未初始化)
var xmlHttp = false;
//创建XMLHttpRequest对象函数
function GetXMLHttpRequest()
{
// 由于 XMLHttpRequest 对象在各个浏览器中实现机制不同,所以要综合考虑
var xmlHttp = false;
//如果是Mozilla浏览器
if (!xmlHttp && typeof XMLHttpRequest != 'object') //undefined
{
xmlHttp = new XMLHttpRequest();
}
// if(window.XMLHttpRequest)
// {
// xmlHttp = new XMLHttpRequest();
// if(xmlHttp.overrideMimeType)
// {
// xmlHttp.overrideMimeType("text/xml");
// }
// }
//如果是IE浏览器
else if(window.ActiveXObject)
{
try
{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e2)
{
xmlHttp = false;
}
}
}
return xmlHttp;
}
function getSecondCategory()
{
var RootCategory = document.getElementById("RootCategory");
//if (RootCategory.Items == null)
//return;
alert("xxx");
xmlHttp = GetXMLHttpRequest();
// 构造将XMLHttpRequest对象将请求的服务器端的URL字符串
var url = "GetSecondCategory.aspx?CategoryID=" + RootCategory.value;
// 建立与服务器端的连接
xmlHttp.open("GET", url, true);
// 设置回调函数
xmlHttp.onreadystatechange = callBack_getSecondCategory;
// 发送请求
xmlHttp.send(null);
}
function callBack_getSecondCategory()
{
var lbl=document.getElementById("lbl");
if (4 == xmlHttp.readyState)
{
alert("4");
if(200 == xmlHttp.status)
{
alert("200");
//通过responseText属性得到服务器端实际返回的文本
lbl.innerHTML =xmlHttp.responseText;
}
}
else
{
lbl.innerHTML =xmlHttp.readyState;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<select id="RootCategory" style="width: 121px" runat="server" onchange="return getSecondCategory();">
<option value="0">--请选择分类--</option>
</select>
<!--二级分类列表 等到ajax查询出该大类的所有子分类,用填充到分类列表中-->
<select id="SecondCategory" runat="server">
<option value="0">--请选择分类--</option>
</select>
<!--三级分类列表 -->
<select id="ThirdCategory" runat="server" >
<option value="0">--请选择分类--</option>
</select>
<span id="lbl"></span>
</div>
</form>
</body>Default页面后台代码如下:
protected void Page_Load(object sender, EventArgs e)
{
BindRootCategory();
} protected void BindRootCategory()
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connstr"].ToString());
SqlCommand cmd = new SqlCommand("select CategoryName,CategoryID from Category where ParentCategoryID=0", con);
try
{
con.Open();
RootCategory.DataSource = cmd.ExecuteReader();
RootCategory.DataTextField = "CategoryName";
RootCategory.DataValueField = "CategoryID";
RootCategory.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
con.Close();
}
}服务器处理页面GetSecondCategory.aspx.cs代码
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
//System.Threading.Thread.Sleep(1000);
String SecondCategory = String.Empty;
SecondCategory = getSecondCategory(); // 发送验证结果
// Response.Clear();
// Response.ContentType = "text/xml";
Response.ContentType = "text/html";
Response.Write(SecondCategory.ToString());
Response.Flush();
Response.Close();
} protected String getSecondCategory()
{
String CategoryName=String.Empty;
using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connstr"].ToString()))
{
SqlCommand cmd = new SqlCommand("select CategoryID,CategoryName from Category where ParentCategoryID=@CategoryID", con);
cmd.Parameters.AddWithValue("@CategoryID", 1);
try
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
CategoryName += reader[1].ToString() + ",";
}
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
return CategoryName;
}
为什么提示服务器端没有返回响应?谢谢!
==
var url = "GetSecondCategory.aspx?CategoryID=" + document.getElementById("RootCategory").options[document.getElementById("RootCategory").selectedIndex].value;
{
if (!IsPostBack)
{
BindRootCategory();
}
}
{
Response.ContentType = "text/html";
Response.Clear();
Response.Write(getSecondCategory());
Response.Flush();
Response.End();
} protected String getSecondCategory()
{
String CategoryName=String.Empty;
using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connstr"].ToString()))
{
SqlCommand cmd = new SqlCommand("select CategoryID,CategoryName from Category where ParentCategoryID=@CategoryID", con);
cmd.Parameters.AddWithValue("@CategoryID", Request.QueryString["CategoryID"]);
try
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
CategoryName += reader[1].ToString() + ",";
}
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
return CategoryName;
}