Adding WebUserControls to table

Discussion in 'ASP .Net Web Controls' started by Svein Terje Gaup, Jun 2, 2004.

  1. 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
    Svein Terje Gaup, Jun 2, 2004
    #1
    1. Advertising

  2. 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

    "Svein Terje Gaup" <> wrote in message
    news:...
    > 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
    >
    >
    Svein Terje Gaup, Jun 3, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Craig Buchanan
    Replies:
    2
    Views:
    347
    Janaka
    Feb 12, 2004
  2. Arjen

    Multiple webusercontrols

    Arjen, Apr 12, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    413
    Arjen
    Apr 13, 2004
  3. tony
    Replies:
    2
    Views:
    311
    tony owen
    Jan 13, 2005
  4. tony

    asp compile and webusercontrols

    tony, Jan 13, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    317
  5. jesusR
    Replies:
    2
    Views:
    463
    jesusR
    Mar 1, 2006
Loading...

Share This Page