Adding WebUserControls to table

  • Thread starter Svein Terje Gaup
  • Start date
S

Svein Terje Gaup

Hi all,

I have a base class that inherits from System.Web.UI.Page. In this base
class I create a table, with placeholders in the table cells.

In the web page that inherits from the base class, I add controls to the
placeholders. This works fine as long as the controls aren't Web User
Controls. This is the problem. Web User Controls do not show up after I've
added them.

In the base class, I override the Render method like this:

protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
writer.RenderBeginTag(HtmlTextWriterTag.Html);
writer.RenderBeginTag(HtmlTextWriterTag.Head);
writer.RenderBeginTag(HtmlTextWriterTag.Title);
writer.Write("TestPage");
writer.RenderEndTag();
writer.RenderEndTag();
MainTable.RenderControl(writer);
writer.RenderEndTag();
base.Render(writer);
}

In MainTable, I've added a placeholder called "Left". I add the
WebUserControl like this in the inheriting class:

private void Page_Load(object sender, System.EventArgs e){
Logout uscLogout = new Logout(); //WebUserControl
uscLogout.ID = "ctrlLogout";
uscLogout.EnableViewState = true;
uscLogout.Visible = true;
Button btn = new Button();
btn.Text = "Click me!";
Test uscTest = new Test();

Left.Controls.Add(uscLogout);
Right.Controls.Add(uscTest);
Left.Controls.Add(btn);
}

MainTable renders as it should, except the WebUserControls I've added to one
of the cells doesn't show up.
If I add a "Button" the same way as I add the WebUserControl, it shows up
just fine.

Also, I had to make remove the "abstract" keyword from the code-behind for
the WebUserControls to be able to instantiate them.

Can anyone spot what's wrong?
Is there a problem with WebUserControls being added like this?

Sincerely
Svein Terje Gaup
 
S

Svein Terje Gaup

Found the solution in the "Portal Starter Kit" at http://www.asp.net.

To insert a UserControl into a PlaceHolder, this is the code to use:
MyPlaceHolder.Controls.Add(Page.LoadControl("~/WebUserControls/UserControl.a
scx"));

Sincerely
Svein Terje Gaup
 

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,770
Messages
2,569,585
Members
45,080
Latest member
mikkipirss

Latest Threads

Top