2

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.

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.
     }

code behind

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; }
}
James123
  • 11,184
  • 66
  • 189
  • 343

1 Answers1

0

one way to do this, is to include a hidden field on the aspx form

<asp:HiddenField ID="jsonDataHolder" ClientIDMode="Static" runat="server"/>

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
}
mfarouk
  • 644
  • 5
  • 14