views:

1689

answers:

1

How do I update a Gridview on a aspx page from a webmethod?

Here is my code.

[WebMethod]
public static string GetDate()
{

    return DateTime.Now.ToString();
}

I can't use the "findcontrol" or the "this" methods so I need some help.

+4  A: 

You can dynamically/programmatically build the GridView control. Then in your WebMethod, you can call RenderControl() method to retrieve the HTML content of the rendered GridView control. Return that content from the WebMethod and have the callback JavaScript function inject the HTML content into your placeholder element's innerHTML property.

Another option is to do your data binding on the client-side. So all the WebMethod has to do is pass back the data (excluding presentational markup), for example in a JSON-type format. Then you can use something like javascript templates to render the grid with data. This approach takes GridView control completely out of the picture, but depending on your needs, it may be a viable option.

Kon