4. Html字符串加载数据测试
1)WebService客户端调用返回Html字符串加载数据:
代码
function bindDataWebServiceHtml() {
var watch = new Stopwatch();
watch.start();
HtmlService.GetUserListHtml(
function(data) {
$("#msg1").html(data);
watch.stop();
$("#time1").html("用时:" + watch.ms + "毫秒.");
});
}
var watch = new Stopwatch();
watch.start();
HtmlService.GetUserListHtml(
function(data) {
$("#msg1").html(data);
watch.stop();
$("#time1").html("用时:" + watch.ms + "毫秒.");
});
}
通过代码,我们可以看到它调用了HtmlService.asmx中Web服务的GetUserListHtml方法:
代码
[WebMethod]
public string GetUserListHtml() {
List list = GetUsers();
StringBuilder builder = new StringBuilder();
for (int i = 0, length = list.Count; i < length; i++)
{
builder.AppendFormat("
UserId:{0}, UserName:{1}, Email:{2}
", list[i].UserId, list[i].UserName, list[i].Email);
}
return builder.ToString();
}
public string GetUserListHtml() {
List list = GetUsers();
StringBuilder builder = new StringBuilder();
for (int i = 0, length = list.Count; i < length; i++)
{
builder.AppendFormat("
UserId:{0}, UserName:{1}, Email:{2}
", list[i].UserId, list[i].UserName, list[i].Email);
}
return builder.ToString();
}
将前端页面对于Html字符串拼接的工作放在WebService中(或者相关后台代码)中去执行,最后通过jQuery的Dom元素html(‘…’)方法赋值。运行加载结果为:

2)jQuery的$.ajax调用返回Html字符串加载数据:
代码
function bindDatajQueryAjaxHtml() {
var watch = new Stopwatch();
watch.start();
$.ajax({
type: "get",
url: "HtmlHandler.ashx",
cache : false,
success: function(data) {
$("#msg3").html(data);
watch.stop();
$("#time3").html("用时:" + watch.ms + "毫秒.");
}
});
}
var watch = new Stopwatch();
watch.start();
$.ajax({
type: "get",
url: "HtmlHandler.ashx",
cache : false,
success: function(data) {
$("#msg3").html(data);
watch.stop();
$("#time3").html("用时:" + watch.ms + "毫秒.");
}
});
}