问题描述
我有JSON数据,我需要将其传递给后面的代码并绑定到obout网格.我知道我们可以使用<WebMethod>
传递数据.但是在Webmethod中,我无法将数据绑定到obout网格和任何网格.因为它是静态的webmethod.
I have JSON data which I need to pass to code behind and bind to obout grid. I know we can pass data using <WebMethod>
. But in Webmethod I cannot bind the data to obout grid and any grid. because it is static webmethod.
所以现在我尝试从javascript调用方法后面的代码,并将数据作为参数传递给方法.我们该怎么做?
So Now I trying to call code behind method from javascript and pass the data as parameter to method. How can we do that?
users = [];
for (var i = 0; i < usersInfo.length; i++) {
user = {
UserName : usersInfo[i].UserName,
Email : usersInfo[i].Email,
Status : status
};
users.push(user);
}
var results = "";
$('#lblError').val('');
if (users.length > 0) {
//Pass the `users` data to ShowResults code behind method.
}
后面的代码
public void ShowResults(List<UsersInfo> users)
{
oboutGrid.DataSource = users;
oboutGrid.DataBind();
}
public partial class UsersInfo
{
public string UserName { get; set; }
public string Email { get; set; }
public string Status { get; set; }
}
推荐答案
一种方法是在aspx表单上包含一个隐藏字段
one way to do this, is to include a hidden field on the aspx form
<asp:HiddenField ID="jsonDataHolder" ClientIDMode="Static" runat="server"/>
在此隐藏字段中,使用JSON.stringify将您的JSON数据添加为字符串
in this hidden field add your JSON data as a string, using JSON.stringify
$('#jsonDataHolder').val(JSON.stringify({ id: 1, name: "mohamed" }));
然后,当页面正常发布回去时,您可以访问隐藏字段,对其进行反序列化并编写代码
then when the page is normally posted back you can access the hidden field, deserilaize it and do your code
protected void btn_DOPostBack_Click(object sender, EventArgs e)
{
string data = this.jsonDataHolder.Value;
// desrialize json, do ur code
}
这篇关于如何将JSON数据传递给方法后面的代码(而不是Webmethod)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!