当前位置: 代码迷 >> Ajax >> ajax留言展示-2011-05-02
  详细解决方案

ajax留言展示-2011-05-02

热度:700   发布时间:2012-10-06 17:34:01.0
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>

  相关解决方案