Can I obtain the rendered html of a gridview via webService call?

Is there a simple of of calling a webService method that Rebinds an asp.Net GridView control and returns its rendered html so that I can refresh the region that contains the gridview on the page?

I can use jQuery.Load maybe but on the server side how can I obtain a specific control’s rendered block?


Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

You can use Gridview.RenderControl() method.

 System.IO.StringWriter stringWrite = new System.IO.StringWriter();    
 System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);    

public override void VerifyRenderingInServerForm(Control control)
       // Confirms that an HtmlForm control is rendered for the
       // specified ASP.NET server control at run time.
       // No code required here.

Solution 2

you can do something like this in the webmethod.

        GridView gv = new GridView();
        gv.AutoGenerateColumns = true;
        //Your Logic to fill dataset/datatable
        DataTable dt=new DataTable();
        dt.Columns.Add(new DataColumn("Index"));
        dt.Columns.Add(new DataColumn("Name"));
        for(int i=0;i<10;i++)
            DataRow row=dt.NewRow();
        //bind the gridview
        gv.DataSource = dt;
        //get the rendered HTML
        StringBuilder sb = new StringBuilder();
        StringWriter writer=new StringWriter(sb);
        HtmlTextWriter txt = new HtmlTextWriter(writer);
        return sb.ToString();

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply