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; }
}
3
  • Why you need to pass JSON object to code-behind and from code-behind bind grid, why can't do that in JS directly? Commented Sep 30, 2014 at 6:19
  • @ArindamNayak ...obout grid doesn't option to bind data from JS. Commented Sep 30, 2014 at 12:50
  • Then you can use updatepanel to do all of this thing, it may not be efficient, but you can give a try! Commented Sep 30, 2014 at 13:07

1 Answer 1

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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.