【问题标题】:i'm trying to bind data from database using javascript ajax function but it didn't work我正在尝试使用 javascript ajax 函数绑定数据库中的数据,但它没有用
【发布时间】:2016-01-14 07:55:13
【问题描述】:

此页面是从另一个页面重定向的结果,并且在 url 中有公司 id

<script type="text/javascript">
    var url = window.location.search.substring(1);
    var CID = url.split("=")[1];//here i take the company id from the link          
    $.ajax({
        type: "POST",
        url: "CompanyPage.aspx/ajaxBindData", 
        contentType: "application/json;charset=utf-8",
        data: '{CID: ' + JSON.stringify(CID) + '}',
        dataType: "json",
         success: function (data) {
             alert(data.d);
             $("#GridView1").data = append(data.d);
             alert("done appending");
             $("#GridView1").bind;
             alert("done binding");
        },
        error: function (exception) {
            alert(exception.responseText );
        }
    });
    </script>

服务器端代码: //服务器端在app_code文件夹中有一个类来执行其中的每个函数

    public static string ajaxBindData(int CID)
    {
        /*
        SqlDataReader rd = EditingEmployee.FillEmps(CompanyID);
        GridView1.DataSource = rd;
        GridView1.DataBind();
        rd.Close();
        */
        DataTable dt = EditingEmployee.GetEmps(CID);

        List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
        Dictionary<string, object> row = null;

        foreach (DataRow dr in dt.Rows)
        {
            row = new Dictionary<string, object>();
            foreach (DataColumn col in dt.Columns)
            {
                row.Add(col.ColumnName, dr[col]);
            }
            rows.Add(row);
        }

        string json = JsonConvert.SerializeObject(rows);
        return json;
    }

编辑员工: //这里是我的项目和数据库的关系

     internal static DataTable GetEmps(int CompId)
     {
         DataTable dt = new DataTable();
         try
         {
             SqlConnection conn = new SqlConnection(connectionString);
             SqlDataAdapter sda = new SqlDataAdapter();
             conn.Open();
             SqlCommand cmd = new SqlCommand("Select Emp_ID,Emp_Name,Company_ID,Emp_Address,Poste_Name, Salary FROM Employee inner join Postes on Postes.PosteID = Employee.Poste_ID Where Company_ID = " + CompId, conn);
             sda.SelectCommand = cmd;
             sda.Fill(dt);
             return dt;

         }
         catch (SqlException ex)
         {
             return null;
         }
     }

【问题讨论】:

  • 你的服务器端代码在哪里?
  • 替换这一行:data: '{CID: ' + JSON.stringify(CID) + '}', WITH data: { 'CID': CID },
  • 它给出了一个新的错误阿米特

标签: javascript c# asp.net json.net


【解决方案1】:

查看此链接以获取解决方案,它是一个 gridview 模板字段绑定方法 http://www.aspforums.net/Threads/122325/Bind-data-to-GridView-with-TemplateField-TextBox-with-jQuery-or-JSON-in-ASPNet/

【讨论】:

    猜你喜欢
    • 2020-12-25
    • 1970-01-01
    • 2018-02-15
    • 1970-01-01
    • 2013-11-04
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 2019-03-23
    相关资源
    最近更新 更多