由于项目要求要用ajax来制作,但是项目中又一直保有datalist,
我一直不知道应该怎么将datalist做到ajax,
于是我上网搜,可是又没有查到.
后来想起来之前做过的一个留言板曾经用到过datalist与页面分离,这个思路让我想到把datalist控件进行HTML的输出就可以做到ajax的交互了.
Code
protected override void Render(HtmlTextWriter writer)
{
string sql = "select count(*) from dbo.tb_Message where EmployeeID=" + loginid.ToString();
int i_RecordCount = int.Parse(DataAccess.GetSingle(sql, null).ToString());
if (i_RecordCount > 0)
{
writer.Write(sp.ToString());
}
this.db.RenderControl(writer);
}
代码只能做参考,不能直接使用,但是这个思路是将render这个方法重写.
Code
var s_GridView_CurrenPage = '0';
var xmlHttp;
var xmlHttpGet;
function GetXmlHttpObject() {
var xmlHttpNew = null;
try {
xmlHttpNew = new XMLHttpRequest();
}
catch (e) {
try {
xmlHttpNew = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
xmlHttpNew = new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttpNew;
}
function GuestBook() {
xmlHttpGet = GetXmlHttpObject();
if (xmlHttpGet == null) {
alert("对不起哦,您的浏览器不支持Ajax!;-)");
return;
}
var url = 'ContactList.aspx';
url = url + "?SID=" + Math.random();
url = url + "&Page=" + s_GridView_CurrenPage;
xmlHttpGet.onreadystatechange = BookGetOK;
xmlHttpGet.open("GET", url, true);
xmlHttpGet.send(null);
}
function BookGetOK() {
if (xmlHttpGet.readyState == 4) {
var gustBookDiv = document.getElementById("listdiv");
gustBookDiv.innerHTML = xmlHttpGet.responseText;
}
}
刚查到网上也有相应的服务器控件与JS的交互,供大家参考,互相学习.