[英]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.