日期:2014-05-16  浏览次数:20677 次

ajax留言显示-2011-05-02

.ashx

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

?

? context.Response.ContentType = "text/plain";
??????????? SqlConnection conn = new SqlConnection("server=.;database=student;uid=sa;pwd=123456");
??????????? conn.Open();
??????????? SqlCommand cmd = new SqlCommand("select * from product",conn);
??????????? SqlDataReader dr = cmd.ExecuteReader();
??????????? DataTable dt = new DataTable();
??????????? dt.Load(dr);

???????
??????????? StringBuilder sb = new StringBuilder();
??????????? for (int i = 0; i < dt.Rows.Count; i++)
???{
?????? sb.Append(dt.Rows[i]["proname"].ToString()).Append("|").Append(dt.Rows[i]["proprice"].ToString()).Append("$");
???}


??????????? context.Response.Write(sb);
??????? }

?

?.js部份

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

?<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
??? <script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
??? <script type="text/javascript">
??????? $.post("Message.ashx", function (data, status) {
??????????? if (status == "success") {
??????????????? var lines = data.split("$");
??????????????? for (var i = 0; i < lines.length; i++) {
??????????????????? var line = lines[i];
??????????????????? var fileds = line.split("|");
??????????????????? var products = $("<li>生产名称:" + fileds[0] + "? 生产价格:" + fileds[1] + "</li>");
??????????????????? $("#ulProduct").append(products);
??????????????? }
??????????? }
??????????? else {
??????????????? $("#ulProduct").append($("<li>加载失败</li>"));
??????????? }
??????? });
??? </script>

?

?

Html

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

<ul id="ulProduct">

</ul>