Using RenderControl to get HTML from a System.Web.UI.UserControl

  • Thread starter Martin Perfelian
  • Start date
M

Martin Perfelian

Hi!

I have a page where I want to programatically call a UserControl and
get the HTML of that control for more processing. I have looked at the
msdn documentation for the RenderControl method, but I can not get the
complete HTML of the control for some reason. The ShortList control
implements two other controls required for the creation of the HTML.
The set-up looks like this:

Page(.aspx)
|
Codebehind(.cs)
|
Create instance of usercontrol ShortList
Call to RenderControl mehtod of ShortList
Put result of RenderControl in a control on Page.


ShortList(.ascx)
|
Codebhind(ascx.cs)
Stuff in Page_Init and Page_Load


What am I doing wrong? What is the correct way of creating an instance
of my control programmatically to make sure the child controls are
created too?

Kind Regards,

Martin Perfelian
 
J

John Saunders

Martin Perfelian said:
Hi!

I have a page where I want to programatically call a UserControl and
get the HTML of that control for more processing. I have looked at the
msdn documentation for the RenderControl method, but I can not get the
complete HTML of the control for some reason. The ShortList control
implements two other controls required for the creation of the HTML.
The set-up looks like this:

Page(.aspx)
|
Codebehind(.cs)
|
Create instance of usercontrol ShortList
Call to RenderControl mehtod of ShortList
Put result of RenderControl in a control on Page.


ShortList(.ascx)
|
Codebhind(ascx.cs)
Stuff in Page_Init and Page_Load


What am I doing wrong? What is the correct way of creating an instance
of my control programmatically to make sure the child controls are
created too?

I cannot speak for user controls specifically, but here are some things to
try for a general problem of "child controls of a dynamic control are not
created".

1) Add the dynamic control to the Controls collection:
this.Controls.Add(dynControl);
2) DataBind the dynamic control: dynControl.DataBind();
3) Reference the dynamic controls Controls collection: int n =
dynControl.Controls.Count;

The trick is to externally trigger the dynamic control into calling its
EnsureChildControls method. Since this method is protected, you can only
attempt to goad the control into calling it - you can't call it directly.

On the other hand, since it's your control, you could add a public method to
the dynamic control which could then call EnsureChildControls:

public void ForceChildControls()
{
this.EnsureChildControls();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top