HelloZeno 2018-12-15 10:00 采纳率: 0%
浏览 1487

C#查询数据库后通过ajax把内容显示在网页上要怎么做?

html页面

    <h2>查询学生</h2>
    <input type="text" id="checkInput" placeholder="请输入要查询学生的姓名关键词:" style="width:200px;height:50px" />
    <button type="button" id="checkBtn" style="width:80px;height:55px">查询</button>
    <h2>显示学生</h2>
    <div id="showDiv"></div>

js代码

$(document).ready(function () {
    $("#checkBtn").click(function () {
        var checkInput = $("#checkInput").val();
        var checkInputJson = "{'checkInput':'checkInput'}"

        $.ajax({
            type: "Post",
            url: "Server.aspx/CheckStudInfo",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: checkInputJson,
            success: function (data) {
                $("#showDiv").html(data.d);
            },
            error: function (err) {
                alert(err)
            }
        })
    });
})

服务器端页面C#总是错误,不知道该怎么入手,求高手

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using System.Data;
using System.Data.SqlClient;

namespace WebApplication1
{
    public partial class Server : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod]
        public static string CheckStudInfo(string checkInput)
        {
            SqlConnection sqlConnection = new SqlConnection();
            string checkSQL = "select * from dbo.StudInfo where StudName like '%" + checkInput + "%'";
            //下面应该如何入手?写出来报错,没有头绪
        }
    }
}
  • 写回答

2条回答 默认 最新

  • cising1689 2018-12-17 05:58
    关注

    把查询出来的数据转换成Json格式
    var json = DataTableToJsonWithJavaScriptSerializer(dt);
    Response.Write(json);
    Response.End();

    #region dataTable通过 JavaScriptSerializer 转换成Json格式
    public string DataTableToJsonWithJavaScriptSerializer(DataTable table)
    {
    JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
    List> parentRow = new List>();
    Dictionary childRow;
    foreach (DataRow row in table.Rows)
    {
    childRow = new Dictionary();
    foreach (DataColumn col in table.Columns)
    {
    childRow.Add(col.ColumnName, row[col]);
    }
    parentRow.Add(childRow);
    }

            return jsSerializer.Serialize(parentRow);
        }
        #endregion 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 outlook无法配置成功
  • ¥15 Pwm双极模式H桥驱动控制电机
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换