由於項目要求要用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的互動,供大家參考,互相學習.