Ajax使用初步,配置步骤
1.把Ajax.dll copy到应用系统bin目录下,然后在工程引用中引用Ajax.dll,如果没有的话可以下载/Files/singlepine/Ajax.rar
2.配置web.config,添加如下信息
<httpHandlers>
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />
</httpHandlers>
3.假设应用系统叫Document,目录格式为Pages\AjaxTest<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />
</httpHandlers>
4.在Pages\AjaxTest下建立cs文件AjaxMethod.cs,添加如下方法
//[Ajax.AjaxMethod]
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
public static string GetNIVNumber(string str)
{
return str;
}
5.在Pages\AjaxTest下建立页面AjaxForm.aspx[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
public static string GetNIVNumber(string str)
{
return str;
}
<HTML>
<HEAD>
<title>AjaxForm</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 language="javascript">
function testAjax()
{
var first=document.getElementById("txtfirst");
AjaxMethod.GetNIVNumber(first.value,callback_GetNIVNumber);
}
function callback_GetNIVNumber(res)
{
var obj=document.getElementById("txtsecond");
obj.value=res.value;
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<INPUT style="Z-INDEX: 101; LEFT: 208px; POSITION: absolute; TOP: 40px" type="button" value="Button"
onclick="testAjax();">
<asp:TextBox id="txtfirst" style="Z-INDEX: 102; LEFT: 40px; POSITION: absolute; TOP: 40px" runat="server"></asp:TextBox>
<asp:TextBox id="txtsecond" style="Z-INDEX: 103; LEFT: 40px; POSITION: absolute; TOP: 72px" runat="server"></asp:TextBox>
</form>
</body>
</HTML>
<HEAD>
<title>AjaxForm</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 language="javascript">
function testAjax()
{
var first=document.getElementById("txtfirst");
AjaxMethod.GetNIVNumber(first.value,callback_GetNIVNumber);
}
function callback_GetNIVNumber(res)
{
var obj=document.getElementById("txtsecond");
obj.value=res.value;
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<INPUT style="Z-INDEX: 101; LEFT: 208px; POSITION: absolute; TOP: 40px" type="button" value="Button"
onclick="testAjax();">
<asp:TextBox id="txtfirst" style="Z-INDEX: 102; LEFT: 40px; POSITION: absolute; TOP: 40px" runat="server"></asp:TextBox>
<asp:TextBox id="txtsecond" style="Z-INDEX: 103; LEFT: 40px; POSITION: absolute; TOP: 72px" runat="server"></asp:TextBox>
</form>
</body>
</HTML>
protected System.Web.UI.WebControls.TextBox txtfirst;
protected System.Web.UI.WebControls.TextBox txtsecond;
private void Page_Load(object sender, System.EventArgs e)
{
//注册,其中Document.Pages.AjaxTest.AjaxMethod为AjaxMethod.cs方法所在的命名空间
Ajax.Utility.RegisterTypeForAjax(typeof(Document.Pages.AjaxTest.AjaxMethod));
}
然后运行就可以了,protected System.Web.UI.WebControls.TextBox txtsecond;
private void Page_Load(object sender, System.EventArgs e)
{
//注册,其中Document.Pages.AjaxTest.AjaxMethod为AjaxMethod.cs方法所在的命名空间
Ajax.Utility.RegisterTypeForAjax(typeof(Document.Pages.AjaxTest.AjaxMethod));
}
实现效果:在第一个textbox中输入内容,点击button,就把内容显示到第二textbox中。这只是一个简单的示例,还可以修改调用方法,从数据库读取以及更复杂的操作