上一步简单的使用了XMLHTTPRequest对象,这次来换个花样,写一下有点样子的代码。
HTML代码,也就是.aspx的,也可以是.html
Code
<!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>无标题页</title>
<script language="JavaScript" type="text/javascript" >
var http_request=false;
function send_request(url)
{
//初始化、指定处理函数、发送请求的函数
http_request=false;
//开始初始化XMLHttpRequest对象
if(window.XMLHttpRequest)
{
//火狐等浏览器
http_request=new XMLHttpRequest();
if(http_request.overrideMimeType)
{//设置MIME类别
http_request.overrideMimeType('text/xml');
}
}
else if(window.ActiveXObject)
{
//IE浏览器
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;
}
http_request.onreadystatechange=processRequest;
http_request.open("GET",url,true);
http_request.send(null);
}
//处理返回信息的函数
function processRequest()
{
if(http_request.readyState==4)
{
//判断对象状态
if(http_request.status==200)
{
//信息已经成功返回,开始处理信息
document.getElementById(currentPos).innerHTML=http_request.responseText;
}
else
{
//页面不正常
alert("您所请求的页面有异常.");
}
}
}
//显示部门下的岗位
function showRoles(obj)
{
document.getElementById(obj).parentNode.style.display="";
document.getElementById(obj).innerHTML="正在读取数据";
currentPos=obj;
send_request("Test2.aspx?playPos="+obj);
}
</script>
</head>
<body>
<table width="200" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="20">
<a href="javascript:void(0)" onclick="showRoles('pos_1')">经理室</a>
</td>
</tr>
<tr style="display:none">
<td height="20" id="pos_1"> </td>
</tr>
<tr>
<td height="20">
<a href="javascript:void(0)" onclick="showRoles('pos_2')">开发部</a>
</td>
</tr>
<tr style="display:none">
<td id="pos_2" height="20"> </td>
</tr>
</table>
</body>
</html>
<!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>无标题页</title>
<script language="JavaScript" type="text/javascript" >
var http_request=false;
function send_request(url)
{
//初始化、指定处理函数、发送请求的函数
http_request=false;
//开始初始化XMLHttpRequest对象
if(window.XMLHttpRequest)
{
//火狐等浏览器
http_request=new XMLHttpRequest();
if(http_request.overrideMimeType)
{//设置MIME类别
http_request.overrideMimeType('text/xml');
}
}
else if(window.ActiveXObject)
{
//IE浏览器
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;
}
http_request.onreadystatechange=processRequest;
http_request.open("GET",url,true);
http_request.send(null);
}
//处理返回信息的函数
function processRequest()
{
if(http_request.readyState==4)
{
//判断对象状态
if(http_request.status==200)
{
//信息已经成功返回,开始处理信息
document.getElementById(currentPos).innerHTML=http_request.responseText;
}
else
{
//页面不正常
alert("您所请求的页面有异常.");
}
}
}
//显示部门下的岗位
function showRoles(obj)
{
document.getElementById(obj).parentNode.style.display="";
document.getElementById(obj).innerHTML="正在读取数据";
currentPos=obj;
send_request("Test2.aspx?playPos="+obj);
}
</script>
</head>
<body>
<table width="200" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="20">
<a href="javascript:void(0)" onclick="showRoles('pos_1')">经理室</a>
</td>
</tr>
<tr style="display:none">
<td height="20" id="pos_1"> </td>
</tr>
<tr>
<td height="20">
<a href="javascript:void(0)" onclick="showRoles('pos_2')">开发部</a>
</td>
</tr>
<tr style="display:none">
<td id="pos_2" height="20"> </td>
</tr>
</table>
</body>
</html>
.aspx.cs的代码,注意,下面的代码和上面的HTML代码不是一个页面
Code
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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;
using System.Xml.Linq;
public partial class Test2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string playPos=Request.Params["playPos"];
if (playPos == "pos_1")
{
Response.Write(" 总经理<br/> 副总经理");
}
else if(playPos=="pos_2")
{
Response.Write(" 总工程师<br/> 软件工程师");
}
}
}
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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;
using System.Xml.Linq;
public partial class Test2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string playPos=Request.Params["playPos"];
if (playPos == "pos_1")
{
Response.Write(" 总经理<br/> 副总经理");
}
else if(playPos=="pos_2")
{
Response.Write(" 总工程师<br/> 软件工程师");
}
}
}