1、asp.net 的C#代码部分
‘[AjaxPro.AjaxMethod]’这句是关键,函数或方法写法没什么特别的地方,只是要使用这句声明这个函数是由AJAX.NET在页面异步请求的,总之记住要通过AJAX获得数据的后台函数就使用[AjaxPro.AjaxMethod]来声明一下。
public partial class Jetflow : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//注册ajax
if (!Page.IsPostBack)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(V3WEB.htm.Test.Jetflow)); ----注册方式要写名命名空间
}
Application["CompanyHtml"] = "hello";
}
//获得显示公司的网页文件
[AjaxPro.AjaxMethod]
public string getCompanyHtml()
{
string _companyname = "jetflow";
if (HttpContext.Current.Application["CompanyHtml"] != null)
{
_companyname = HttpContext.Current.Application["CompanyHtml"].ToString(); -----读写全局变量要用HttpContext.Current.Application方式
}
return _companyname;
}
//判断用户名密码是否正确
[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)] // 读写Session: 加上此句才可读写session
public int isRight(string userID, string userPwd)
{
int count = 0;
Comm_Grant_UserBLL obj = new Comm_Grant_UserBLL();
DataTable u = obj.getIsRight(userID, userPwd);
if ( u.Rows.Count > 0)
{
Session["UserID"] = u.Rows[0]["UserID"].ToString(); -------写Session
Session["UserName"] = u.Rows[0]["UserName"].ToString();
Session["Login"] = "Yes";
count = 1;
}
else
{
count = 0;
}
return count;
}
}
2、前台javascript部分(测试部分)
<head >
<title></title>
<script type="text/javascript">
function OpenHtml() {
var companyhtml = V3WEB.htm.Test.Jetflow.getCompanyHtml().value;
if (companyhtml == "jetflow") {
document.getElementById("remark").innerHTML = "<b>"+"jetflow"+"</b>";
document.getElementById("mainform").innerHTML = '<iframe id="MainFrame" width="612" height="574" src="/htm/test/companyHtml.htm" frameborder="1" scrolling="no"></iframe>';
}
else {
document.getElementById("remark").innerHTML = "<b>" + companyhtml + "</b>";
document.getElementById("mainform").innerHTML = '<iframe id="MainFrame" width="612" height="574" src="/htm/homepage.htm" frameborder="1" scrolling="no"></iframe>';
}
}
//var readspeed = 40000; //读取转换页面时间
//var changeHtml = setInterval(OpenHtml, readspeed); //设置页面转换频率
</script>
</head>
<body>
<form id="Form1" runat="server">
<div id="remark">123</div><div><input id="btnGet" type="button" value="获取" onclick="OpenHtml();" style="width: 75px" class="btn"/></div>
<div id="mainform"></div>
</form>
</body>