简体   繁体   English

我正在尝试使用javascript ajax函数绑定数据库中的数据,但是它不起作用

[英]i'm trying to bind data from database using javascript ajax function but it didn't work

this page is the result of redirection from another page and have the company id in the 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>

server side code : //the server side have an class in app_code folder to execute every function in it 服务器端代码://服务器端在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;
    }

editingemployee : //here is the relation between my project and the database editingemployee://这里是我的项目和数据库之间的关系

     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;
         }
     }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从servlet传递参数到javascript函数,在查看数据库时不起作用? - pass parameter from servlet to function javascript , it didn't work when view database? 当我从另一个函数使用它时,Javascript 排序不起作用 - Javascript sort doesn't work when i'm using it from another function 使用AJAX的jQuery自动完成功能无效 - jQuery Autocomplete using AJAX didn't work 我正在尝试使用jquery和ajax将多个数据发送到另一个页面,以检查数据库中是否存在值 - I'm trying to send multiple data using jquery and ajax to another page to check if a value is present in database or not 我正在尝试在文本区域中进行特定的输入,调用特定的javascript函数,但是它将无法正常工作 - I'm trying to make a specific input into a text area call a specific javascript function, but it won't work 我正在使用正则表达式功能,但它不起作用? - i am using regex function but it didn't work? javascript功能在Chrome中不起作用 - javascript function didn't work in chrome Angular服务试图从文件中读取数据(我正在使用node-webkit),无法使其正常工作 - Angular service, trying to read data from file (I'm using node-webkit), can't get it to work javascript 函数在 php echo 中不起作用 - javascript function didn't work at php echo If and else function 在 javascript 上不起作用 - If and else function didn't work on javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM