Thursday, September 22, 2011

Get HTML of server side control from ajax / webservice call.
This is used full when we have to show grid on page and fill operation needs a ajax funcaiton. than we can just bind grid with datasource and get its rendring HTML to send the response on called function.

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(); row["Index"] = i; row["Name"] = "dummyData" + i.ToString(); dt.Rows.Add(row); }
      //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();