p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

proasp_codeclinic thread: User Control Problem - "Control '_ctl0_<ID>' of type 'Button' mu st be placed inside a form tag with runat=server. "


Message #1 by "Goodwin, D (David)" <GoodwinD@r...> on Wed, 6 Nov 2002 11:48:05 -0000
You can probably do what you wanted to do, but you'd need to use the
Page.Controls.AddAt() method rather than the Page.Controls.Add() method. It
seems (to me) that ASP.Net is adding the control before the <form> and that
is causing a problem because you have a HTML or Web control in your user
control.

Instead, you need to find the index of <form>, and the use AddAt() to add it
after this. Alternatively, you could stick a placeholder into your page in
the correct place, and add your control to the placeholder's Controls
collection...

Cheers
Ken

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From: "Goodwin, D (David)" <GoodwinD@r...>
Subject: [proasp_codeclinic] Re: User Control Problem - "Control '_ctl
0_<ID>' of type 'Button' mu st be placed inside a form tag with runat=se r
ver. "


: Thanks
: asp:placeholder is the key.
: Cheers
: Dave.
:
: -----Original Message-----
: From: Ken Schaefer [mailto:ken@a...]
: Sent: 08 November 2002 04:35
: To: Code Clinic
: Subject: [proasp_codeclinic] Re: User Control Problem - "Control '_ctl
: 0_<ID>' of type 'Button' mu st be placed inside a form tag with
: runat=ser ver. "
:
:
: Not an expert, but the way I've always done this is something like:
:
: <script>
: Dim ctlMyControl as Control
:
: ctlMyControl = LoadControl("myControl.ascx")
: plhMyPlaceholder.Controls.Add(ctlMyControl)
: </script>
:
: <form runat="server">
:     <asp:placeholder id="plhMyPlaceholder" runat="server" />
: </form>
:
: so the <asp:placeholder> is between the <form></form> tags...
:
: However, with the caveat at the top, it may be better to take this
: discussion to one of the ASP.Net lists as you're more likely to get a
better
: answer!
:
: Cheers
: Ken
:
:
: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: From: "Goodwin, D (David)" <GoodwinD@r...>
: Subject: [proasp_codeclinic] Re: User Control Problem - "Control '_ctl
: 0_<ID>' of type 'Button' mu st be placed inside a form tag with runat=ser
: ver. "
:
:
: : Ken
: : Thanks for the response..
: :
: : For the Page that uses the control, in it's Page_Load Event I'm using
the
: : syntax  :-
: :
: : Protected WithEvents DGBrowseOptions as WUCDataSetTabOptions
: : DGBrowseOptions = Page.LoadControl("WUCParamertizer.ascx")
: : Page.Controls.Add(DGBrowseOptions)
: : DGBrowseOptions.Doit  'this is the function that tells the control to
: create
: : the buttons.
: :
: : so when you ask
: : "Are you adding the user control between <form runat="server"></form>
: tags,
: : or outside?"
: :
: : I guess the answer is  it has no entry. Theres no entry between these
tags
: : because it's added at runtime in the Pages Page_Load event. If I add the
: : user control to my page at design time, it will have an entry and by
: adding
: : a corresponding variable in the code behind form it'll work just fine.
Do
: I
: : need to have an entry for this user control between the form tags when
I'm
: : creating it at runtime?


  Return to Index