- 爱易网页
-
ASP.NET教程
- jquery调用ashx文件中返回值的有关问题
日期:2014-05-17 浏览次数:20513 次
jquery调用ashx文件中返回值的问题
js:
控件:<input type=text onblur="checkdata(this)" id="wldm'+ newRowIndex +'" name="wldm'+newRowIndex+'"/>
注:控件id是动态生成
function checkdata(obj)
{
var tb_id=obj.id;
var tb_text=obj.value;
$("#ctl00_ContentPlaceHolder1_ddl_DP").empty();
$.getJSON("DPData.ashx?value="+tb_text+"¶=" + Math.random().toString(),null,function(json){
$.each(json,function(i){$("#"+tb_id).val(json[i].ID);});
});
}
ashx文件:
public class DPData : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
context.Response.Write(Result(context));
}
public StringBuilder Result(HttpContext context)
{
string connect = System.Configuration.ConfigurationManager.ConnectionStrings["connect"].ConnectionString;
SqlConnection con = new SqlConnection(connect);
con.Open();
string value = context.Request.QueryString["value"];
string sql = "select ID,DPName from DPTable where id='" + value + "'";
SqlDataAdapter dap = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
dap.Fill(ds, "Table");
StringBuilder strClass = new StringBuilder();
if (ds.Tables[0].Rows.Count > 0)
{
strClass.Append("[");
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
strClass.Append("{");
strClass.Append("\"ID\":\"" + ds.Tables[0].Rows[i]["ID"].ToString() + "\",");
strClass.Append("\"DPName\":\"" + ds.Tables[0].Rows[i]["DPName"].ToString() + "\"");
if (i != ds.Tables[0].Rows.Count - 1)
{