i';我试图使用javascript ajax函数绑定数据库中的数据,但它没有';行不通
此页面是从另一个页面重定向的结果,url中有公司idi';我试图使用javascript ajax函数绑定数据库中的数据,但它没有';行不通,javascript,c#,asp.net,json.net,Javascript,C#,Asp.net,Json.net,此页面是从另一个页面重定向的结果,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: "
<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>
查看此链接了解解决方案它是一个gridview templatefield绑定方法
服务器端代码在哪里?请参阅将此行替换为:data:'{CID:'+JSON.stringify(CID)+'},数据为:{CID':CID},它会给出一个新的错误Amit
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;
}
}