20151229:AJax :用asp.net方式做一个查询

aspx代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="Scripts/jquery.min.js"></script>
</head>
<body>        
    <form id="form1" runat="server">
    <div>
        <h1>员工表</h1>
        <br />
        <input id="txtname" type="text" />
        <input id="btn" type="button" value="查询" />
        <br />
        <br />
        <br />
        <table id="tabinfo" width="600" border="0" cellspacing="1" cellpadding="1" bgcolor="#6600FF">
            <tr id="top">
                <td width="120" height="30" align="center" valign="middle" bgcolor="#FFFFFF">代号</td>
                <td width="120" align="center" valign="middle" bgcolor="#FFFFFF">姓名</td>
                <td width="120" align="center" valign="middle" bgcolor="#FFFFFF" >性别</td>
                <td width="120" align="center" valign="middle" bgcolor="#FFFFFF">民族</td>
                <td width="120" align="center" valign="middle" bgcolor="#FFFFFF">生日</td>               
            </tr>
            <tr>
            <table id="tab" width="600" border="0" cellspacing="1" cellpadding="1" bgcolor="#6600FF">
                <%--asp.net方式,在HTML里面嵌入C#代码--%>
                <%
                    TestDataContext context = new TestDataContext();//查数据库
                    var query = context.Info;
                    foreach( Info data in query )//遍历每一行数据造tr
                    { 
                     %>
                    <tr>
                        <td width="120" height="30" align="center" valign="middle" bgcolor="#FFFFFF"><%=data.Code %></td>
                        <td width="120" align="center" valign="middle" bgcolor="#FFFFFF"><%=data.Name %></td>
                        <td width="120" align="center" valign="middle" bgcolor="#FFFFFF" ><%=data.Sex %></td>
                        <td width="120" align="center" valign="middle" bgcolor="#FFFFFF"><%=data.Nation %></td>
                        <td width="120" align="center" valign="middle" bgcolor="#FFFFFF"><%=data.Birthday %></td> 
                    </tr>
                <%
                    }
                     %>
             </table>
             </tr>
        </table>
    </div>
    </form>
     <script type="text/javascript">
         $(document).ready(function (e) {
            //加点击事件
             $("#btn").click(function (e) {
                 //取值
                 var name = $("#txtname").val();
                 //调AJAX
                 $.ajax({

                     url: "Select.ashx",//处理页面
                     type: "POST",//数据提交的方式,共两种POST,GET
                     data: { name: name },//要传输的数据,JSON格式
                     datatype: "TEXT",//返回的数据格式,共三种TEXT,JSON,XML
                     success: function (data) { //回调函数

                         //alert(data);
                         var hang = data.split('|');//拆出行
                         var text = "";
                         for (var i = 0; i < hang.length; i++)
                         {
                             var lie = hang[i].split('^');
                             text = text + "<tr height='30'>";
                             for (var j = 0; j < lie.length;j++)
                             {
                                 text = text + "<td  width='120' align='center' valign='middle' bgcolor='#FFFFFF'>" + lie[j] + "</td>";
                             }
                             text = text + "</tr>";
                         }
                         $("#tab").html(text);
                     }
                 });
             })
         })
    </script>
</body>
</html> 

处理页面ashx后台c#代码:

<%@ WebHandler Language="C#" Class="Select" %>

using System;
using System.Web;
using System.Data;//引用命名空间
using System.Linq;
using System.Data.Linq;
using System.Data.SqlClient;

public class Select : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        //取值
        string name = context.Request["Name"].ToString();
        
        SqlConnection conn = new SqlConnection("server=.\\SQL;database=mydb;uid=sa;pwd=123");
        SqlCommand cmd = conn.CreateCommand();

        cmd.CommandText = "select *from Info where Name like  '%" + name + "%'";
        conn.Open();    
        SqlDataReader _dr = cmd.ExecuteReader();
        string shuju="";
        while(_dr.Read())//遍历每一行
        {
            for (int i = 0; i< _dr.FieldCount;i++)//遍历每一列
            {//某一列的值_dr[i] 把数据里面拼上每一列的数据
                shuju += _dr[i].ToString() +"^";
            }
            shuju = shuju.Substring(0, shuju.Length - 1);
            shuju +="|";
        }
        shuju = shuju.Substring(0, shuju.Length - 1);//去掉最后一个"|"
        conn.Close();
        context.Response.Write(shuju);
        context.Response.End();
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

 

查询结果:

posted @ 2015-12-29 23:03  m-n  阅读(263)  评论(0编辑  收藏  举报