Problem dynamically loading a user control....

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • José Joye

    Problem dynamically loading a user control....

    Hello,

    I'm playing around with dynamically loading user controls ...and having
    problems

    I created a really simple userControl (in fact contains a plain text box)
    and placed it into the MyTestCtrl2.asc x file.
    In an aspx file I placed the following in the Page_Load method:


    private void Page_Load(objec t sender, System.EventArg s e)
    {
    Control uc2 = LoadControl("My TestCtrl2.ascx" );
    Page.Controls.A dd(uc2);
    }


    However, I'm always getting this error:


    Control '_ctl0_TextBox1 ' of type 'TextBox' must be placed inside a form tag
    with runat=server.
    Description: An unhandled exception occurred during the execution of the
    current web request. Please review the stack trace for more information
    about the error and where it originated in the code.

    Exception Details: System.Web.Http Exception: Control '_ctl0_TextBox1 ' of
    type 'TextBox' must be placed inside a form tag with runat=server.

    Source Error:

    An unhandled exception was generated during the execution of the current web
    request. Information regarding the origin and location of the exception can
    be identified using the exception stack trace below.

    Stack Trace:


    [HttpException (0x80004005): Control '_ctl0_TextBox1 ' of type 'TextBox' must
    be placed inside a form tag with runat=server.]
    System.Web.UI.P age.VerifyRende ringInServerFor m(Control control)
    System.Web.UI.W ebControls.Text Box.AddAttribut esToRender(Html TextWriter
    writer)
    System.Web.UI.W ebControls.WebC ontrol.RenderBe ginTag(HtmlText Writer
    writer)
    System.Web.UI.W ebControls.Text Box.Render(Html TextWriter writer)
    System.Web.UI.C ontrol.RenderCo ntrol(HtmlTextW riter writer)
    System.Web.UI.C ontrol.RenderCh ildren(HtmlText Writer writer)
    System.Web.UI.C ontrol.Render(H tmlTextWriter writer)
    System.Web.UI.C ontrol.RenderCo ntrol(HtmlTextW riter writer)
    System.Web.UI.C ontrol.RenderCh ildren(HtmlText Writer writer)
    System.Web.UI.C ontrol.Render(H tmlTextWriter writer)
    System.Web.UI.C ontrol.RenderCo ntrol(HtmlTextW riter writer)
    System.Web.UI.P age.ProcessRequ estMain()





    Thanks,
    José


  • José Joye

    #2
    Re: Problem dynamically loading a user control....

    Thanks,
    This works!

    However, I have seen tons of samples that add the userControl to the Control
    collection of the Page (including in MSDN).
    Is this incorrect?

    José
    "srini" <srini@discussi ons.microsoft.c om> wrote in message
    news:68ADD422-9AA9-4232-8727-023FB1116277@mi crosoft.com...[color=blue]
    > hi Jose,
    > One way is to add a placeholder to your page at design time and later add
    > the controls to this placeholder instead of adding the controls to the
    > page.
    > instead of page.controls.a dd use placeholder1.co ntrols.add.
    > hope that helps
    > the best
    > srini
    >
    > "José Joye" wrote:
    >[color=green]
    >> Hello,
    >>[/color]
    >[/color]


    Comment

    Working...