Ajax.net 使用入门
以下在.net1.1中通过.
1.添加对AjaxPro.dll的引用
2.在web.config 中的<system.web>中添加配置:
<httpHandlers>
<add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory, AjaxPro"/>
</httpHandlers>
2.程序中:
以下演示从数据库中取出数据,并在前台显示出表中的第一个单元格的数据。
DataDemo.aspx.cs:
private void Page_Load(object sender, System.EventArgs e)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(DataDemo));
}
[AjaxPro.AjaxMethod]
public DataSet GetDataSet()
{
string conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "DB\\AjaxDemo.mdb";
OleDbConnection con = new OleDbConnection(conStr);
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter("select * from student",conStr);
da.Fill(ds);
return ds;
}
DataDemo.aspx:{
AjaxPro.Utility.RegisterTypeForAjax(typeof(DataDemo));
}
[AjaxPro.AjaxMethod]
public DataSet GetDataSet()
{
string conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "DB\\AjaxDemo.mdb";
OleDbConnection con = new OleDbConnection(conStr);
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter("select * from student",conStr);
da.Fill(ds);
return ds;
}
<script>
function showData()
{
var dataSrc = AjaxProDemo.DataDemo.GetDataSet();
var cols = dataSrc.value.Tables[0].Columns.length;
var rows = dataSrc.value.Tables[0].Rows.length;
alert(cols + " cols and " + rows + " rows");
}
function showAll()
{
var dataSrc = AjaxProDemo.DataDemo.GetDataSet();
var cell = dataSrc.value.Tables[0].Rows[1].sname;
alert(cell);
}
</script>
//
<INPUT type="button" value="ShowData" onclick="showData()">
<INPUT type="button" value="ShowAll" onclick="showAll()">
function showData()
{
var dataSrc = AjaxProDemo.DataDemo.GetDataSet();
var cols = dataSrc.value.Tables[0].Columns.length;
var rows = dataSrc.value.Tables[0].Rows.length;
alert(cols + " cols and " + rows + " rows");
}
function showAll()
{
var dataSrc = AjaxProDemo.DataDemo.GetDataSet();
var cell = dataSrc.value.Tables[0].Rows[1].sname;
alert(cell);
}
</script>
//
<INPUT type="button" value="ShowData" onclick="showData()">
<INPUT type="button" value="ShowAll" onclick="showAll()">
如果数据源是对象数组,那就请看下面的代码:
var arrStaffsSearchResult = StaffSelecter.SearchStaffs(sSearchKeyword);
lstUnSelectedStaffList.options.length = 0;
if(arrStaffsSearchResult.value==null || arrStaffsSearchResult.value.length==0)
return;
for(var i=0;i<arrStaffsSearchResult.value.length;i++)
{
var currStaffs = arrStaffsSearchResult.value[i];
var oNewItem = document.createElement("OPTION");
oNewItem.value = currStaffs.ID;
oNewItem.text = "[" + currStaffs.StaffNo + "]" + currStaffs.Name;
lstUnSelectedStaffList.add(oNewItem);
lstUnSelectedStaffList.selectedIndex = 0;
currConditionAllStaffsObj.Add(oNewItem.value,currStaffs);
}
lstUnSelectedStaffList.options.length = 0;
if(arrStaffsSearchResult.value==null || arrStaffsSearchResult.value.length==0)
return;
for(var i=0;i<arrStaffsSearchResult.value.length;i++)
{
var currStaffs = arrStaffsSearchResult.value[i];
var oNewItem = document.createElement("OPTION");
oNewItem.value = currStaffs.ID;
oNewItem.text = "[" + currStaffs.StaffNo + "]" + currStaffs.Name;
lstUnSelectedStaffList.add(oNewItem);
lstUnSelectedStaffList.selectedIndex = 0;
currConditionAllStaffsObj.Add(oNewItem.value,currStaffs);
}