SpoiledTechie.com
SpoiledTechie.com

Reputation: 10725

How do I Update a GridView from a Page Method from code behind?

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.

Upvotes: 4

Views: 3938

Answers (1)

Kon
Kon

Reputation: 27441

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.

Upvotes: 5

Related Questions