处理程序的回调函数中有多个值.如何分别绑定这些值

Multiple values in callback function from handler. How to bind these values sepretly

本文关键字:绑定 回调 程序 函数 处理      更新时间:2023-09-26

Like处理程序将这些返回值。。(ds)作为数据集

context.Response.Write(ds.Tables[0].Rows[0]["name"]);
context.Response.Write(ds.Tables[0].Rows[1]["age"]); 

然后如何绑定它们或秘密查找值。对于单个值,我使用

function callback(data)
{
     $("#Lbl").html(data);
}

但对于多个值?????

从你的问题中很难理解你想做什么,但根据我对这个问题的理解,我可能会把数据变成JSON,

通过使用asp.net的内置JSON功能,您可以更优雅地完成这项工作,但现在请更改处理程序以编写以下JSON:

context.Repsonse.Write("{'"name'":'"" + ds.Tables[0].Rows[0]["name"]) + "'",";
context.Repsonse.Write("{'"age'":'"" + ds.Tables[0].Rows[1]["age"]) + "'"}";

输出应该是:

  {"name":"name","age":"69"}

然后你可以在客户端上做这样的事情

function callback(data)
   {
     var data = $.parseJSON(data);
     $("#name").html(data.name);
     $("#age").html(data.age);
   }

edit:添加了更多细节

您可以根据需要设置数据。您可以构建一个逗号分隔的值字符串:

string res = ds.Tables[0].Rows[0]["name"];
res += ";" + ds.Tables[0].Rows[0]["age"];
//and so on...
//remember to check null values and casting
Response.Write(res);

或者您可以创建一个JSON字符串:

res = string.format("'name' : '{0}', 'age' : '{1}'", ds.Tables[0].Rows[0]["name"], ds.Tables[0].Rows[0]["age"]);

例如,客户端可以创建这样一个结构:

$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: callback
});

如果您选择逗号分隔的列表,回调必须处理一些字符串操作(.split()方法可能是少数)。如果您选择使用JSON,请参阅http://api.jquery.com/jQuery.getJSON/