.NET AJAX无刷新处理数据集合

来源:互联网 发布:东莞进销存软件 编辑:程序博客网 时间:2024/04/19 12:41

想做一个.NET AJAX调用WEB服务数据集合的显示数据的功能,找了好久,硬是没找到比较好的例子,自己摸索了一下,终于搞定了~共享下方法,数据集处理上应该也和IList<>是一个样的;大家可以参考下。

后台代码:

 [System.Web.Services.WebMethod]
    public static IList<Send> ShowSends()
    {
        SqlConnection mycon = new SqlConnection();
        mycon.ConnectionString = @"Data Source=SHAOSHAO-PC;Initial Catalog=G:/SSHOP/APP_DATA/SSHOP.MDF;Integrated Security=True";
        SqlCommand mycmd = new SqlCommand();
        mycmd.CommandText = "getsends";
        mycmd.CommandType = CommandType.StoredProcedure;
        mycmd.Connection = mycon;
        mycon.Open();
        SqlDataReader myre = mycmd.ExecuteReader();
        IList < Send > sends= new List<Send>();
        while (myre.Read())
        {
            Send send = new Send();
            send.Id = myre["sendid"].ToString();
            send.Adminname = myre["adminname"].ToString();
            send.Time = myre["time"].ToString();
            send.Sendtext = myre["sendtext"].ToString();
            send.Sendgut = myre["sendgut"].ToString();
            sends.Add(send);
        }
        return sends;
    }

HTML代码:

<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
        </asp:ScriptManager>
    </div>
    </form>
    <p>
        <input id="Button1" type="button" value="显示" onclick="return Button1_onclick()" /></p>
           <div id="showsends">
        </div>
</body>

JS脚本:

function Button1_onclick() {

PageMethods.ShowSends(show);
}
function show(result)
{
var contentBuilder = new Sys.StringBuilder();
contentBuilder.append("<table border=/"1px/" >");
for (var i = 0; i < result.length; i++)
{
contentBuilder.append("<tr>");
contentBuilder.append("<td>"+result[i].Id+"</td>");
contentBuilder.append("<td>"+result[i].Adminname+"</td>");
contentBuilder.append("<td>"+result[i].Time+"</td>");
contentBuilder.append("<td>"+result[i].Sendtext+"</td>");
contentBuilder.append("<td>"+result[i].Sendgut+"</td>");
contentBuilder.append("</td>");
}
document.getElementById("showsends").innerHTML=contentBuilder.toString();
}

运行效果为点击显示按钮则显示一个表格。JS与.NET结合还是挺完美的~MS也不完全是垃圾的~

原创粉丝点击