2

I need to get the HTML of a user control.

At the moment I am using below code.

// Approach 1
HeaderControl hControl = new HeaderControl();

StringBuilder b = new StringBuilder();
HtmlTextWriter h = new HtmlTextWriter(new StringWriter(b));

hControl.RenderControl(h);
string controlAsString = b.ToString();

// controlAsString  is "" -- Doesn't work

// ----------------------------------------------
// Approach 2
UserControl uc = new UserControl();
HeaderControl hc = (HeaderControl)uc.LoadControl("~/Views/HeaderControl.ascx");

hControl.RenderControl(h);
string controlAsString = b.ToString();

// controlAsString = "<h3>test data</h3>  - Works.

Can you please explain how I can achieve this using the approach 1 So that I dont have to hard code the virtual path of the control.

I have also tried the other overload of the uc.LoadControl()

UserControl uc = new UserControl();
HeaderControl hControl = (HeaderControl)uc.LoadControl(typeof(HeaderControl), null);

// Header control has a default constructor that takes no parameters

// but no luck :(
123 456 789 0
  • 10,565
  • 4
  • 43
  • 72
Menol
  • 1,230
  • 21
  • 35

1 Answers1

0

bshould be a System.IO.StringWriter. Your code in approach 1 should look like this:

var hControl = new HeaderControl();
var strWriter = new System.IO.StringWriter();
var htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
hControl.Rendercontrol(htmlWriter);
string ControlAsString = strWriter.ToString();

This is copied from a piece of code I've written with help of another answers here in SO, and works perfectly for me.

jose
  • 637
  • 6
  • 20
  • Thanks Jose, but no luck. var hControl = new HeaderControl(); var strWriter = new System.IO.StringWriter(); var htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter); hControl.RenderControl(htmlWriter); string ControlAsString = strWriter.ToString(); ControlAsString is still "" – Menol Oct 08 '13 at 07:46
  • Well, I've just tried the same code here, with a newly created control, and it worked as expected, returning ``. Are you sure your control is actually returning any html from the rendering? Could you try testing the code on your environment with a newly created control, please? – jose Oct 08 '13 at 12:05
  • Yes Jose, The control works when I use the path to invoke. I think the problem is the control does not get loaded just because it was instantiated. Could you please check your code to see if you do something else to "load" the component. If I may ask, is it possible to post your complete code (related)? – Menol Oct 08 '13 at 15:16
  • In the code I have in production I do populate the control before rendering it. It's a pretty big method, so it's not viable to post it here. The only difference is that the control in question is a gridview. However, I have tested this piece of code with a completely new and blank control, and it worked. – jose Oct 08 '13 at 15:31
  • Jose, I can promise you my control has a very simple header text and it does get loaded when I use the load method with virtual path. I read some articles and they say that a user control can only be loaded by 1 - using the load method with the virtual path 2 - create a uc object and add it to a page which triggers loading the uc. Is it possible that your code does the 2nd thing? – Menol Oct 08 '13 at 16:35