aleafonso
aleafonso

Reputation: 2256

How to bind a GridView after calling a WebMethod?

At the moment I am doing drag & drop to GridView rows which sends the new row order to a WebMethod in order to update the database according to these new values.

So, how can I update the GridView in the web form after calling the WebMethod? Considering that I can't access to any of the webform elements from a WebMethod.

Web Method:

[WebMethod]
public static void GridViewCarriersReorders(string Reorder)
{
    Boolean result;
    string[] ListID = Reorder.Split('|');
    transactions tr = new transactions();
    result = tr.updateLcrPriorities(ListID);
    //updateGridViewCarriers(); //SOMETHING LIKE THIS IS NEEDED TO DO THE BINDING
}

Thanks a lot for your help and comprehension.

UPDATE: This is how I am calling the webmethod:

$.ajax({
    type: "POST",
    url: "lcrP.aspx/GridViewCarriersReorders",
    data: '{"Reorder":"' + strorder + '"}',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    async: true,
    cache: false,
    success: function (msg) {
        $.jGrowl("The Carriers priority was successfully updated", { theme: "succeeded" });
    }
})

Upvotes: 1

Views: 5020

Answers (1)

onof
onof

Reputation: 17377

If you put an UpdatePanel around the gridview, you can call a postback only for it:

success: function (msg) {
        $.jGrowl("The Carriers priority was successfully updated", { theme: "succeeded" });
        __doPostBack('UpdatePanel1', '');
    }

EDIT: As you found by yourself, i missed writing that in the OnLoad event you should force databind of the grid:

void OnLoad(EventArgs e) {
    base.OnLoad(e);
    myGrid.DataBind();
}

As an alternative, if your grid is bound to a xDataSource control you can disable the viewstate of the grid, and if it will be forced to DataBind at each postback.

Upvotes: 1

Related Questions