Control 'X' of type 'RadioButton' must be placed inside a form tag with runat=server.

  • Thread starter Thread starter Tom wilson
  • Start date Start date
T

Tom wilson

I'm trying to create dynamic controls in ASP.Net. It's driving me
nuts. I keep getting the error:

Control '16' of type 'RadioButton' must be placed inside a form tag
with runat=server.

Dim Place1 As New PlaceHolder
Controls.Add(Place1)
For y = 1 To Choices.RecordCount
Choices.MoveTo(y)
Dim Radio As New RadioButton
Radio.ID = choices.ID
Radio.Text = choices.ChoiceText
Radio.GroupName = Q.ID
Place1.Controls.Add(Radio)

If I do Controls.Add(Radio) I get the above error. So I created a
placeholder in design view (there's nothing there since all page
content is dynamic). It added the radios but at the bottom of the
form. So I modified the code to dynamically create the placeholder
(as above). I STILL get this error.

Do I have to do a:
response.write("<FORM etc etc... runat=server>")
before every checbox creation?

Why doesn't it add the tags itself while its creating the controls?

How do I add a control without getting that needless error at every
turn?

Thanks!
 
Hi Tom,

Try this instead of Controls.Add(Place1):

Me.FindControl("Form1").Controls.Add(Place1)

(provided your form is called Form1, obviously, but that's what it's
called by default).

You were adding the radiobuttons in the Page controls, but outside of
the Form controls.

Also note that "Me." is only here for demonstration purposes. You can
omit it, it's implicit.

HTH,

Michel
 
When I add that line I get "Object reference not set to..." I've
tried it on its own page with nothing else but this:

Dim Place1 as new Placeholder
me.FindForm("TestForm").Controls.Add(Place1)

Same result. I have discovered that I can add a placeholder in design
view and it will accept control additions. I guess the whole problem
is that I cannot create a placeholder dynamically within the form
tags. I don't know why something so simple has to be so complicated.
 
Couple things.

1. What is your page derived from to have a FindForm method?
2. Your form is probably called frmTestForm.

me.FindControl( "frmTestForm" ).Controls.Add( Place1 )

It really isn't that complicated; it is simply a matter of learning the
framework. Although I do wish MS would have exposed the internal Form
property they have. Perhaps this will be modified in 2.0.

bill
 
1 - How do I find that out? The Class is called "TestForm". It
appears to inherit from "MyApp.TestForm". Is that what I need?


Not complicated? This is a re-write of a survey application
originally done in asp. For that method, it was as simple as one line
of HTML to create a textbox and response.write the text/html. With
this I have to:

1 - Manually add a placeholder to the empty form. I can't create one
dynamically, everyt method I've tried fails.

2 - Create a set of control arrays for each type of control; checkbox,
radio, etc.

3 - Redim Preserve each array as the number of controls increases as
they are created. I hope 500 radios don't eat too much memory.

4 - Create the control, takes about 5 lines of code not including the
array tracking.

5 - Add the newly created control to the placeholder.

....and everything must be a control. Text? HTML? They have to be
defined as LiteralControl(s) and added in sequence. I remember when
it was called "response.write" but if I do that, all the text is at
the top of the page and the controls underneath because response.write
doesn't add to a placeholder. I swear if I have to do tables my head
will explode.

Yes, it's that complicated.

Sorry about the rant, it's been 3 days at this with little progress.
I do very much appreciate your input.
 
1 - look at the aspx side of the page.

You will see the form element.

<form runat="server" id="myForm">

You can get this control by typing
Form = Page.FindControl( "myForm" );

Notice the parameter to FindControl is the ID of the control. If ID isn't
there, then assign it one.

ASP.NET is a completely different way of html generation. You really can't
think of things such as, "in asp I would just do document.write, etc." You
must think of terms of controls.

I really don't know enough about your requirements to tell you how and what
you should be doing, but I wish you the best of luck. There are several
ways to accomplish what you are trying to do.

Is the entire page dynamic? What determines the content? How much state
information should be maintained.

bill

You can always access the Request.Forms("txtName") and get the posted values
without worry about setting up control trees.
Also, if you really like the asp rendering module, you can always override
the Page.Render method and write out the html using Response.Write.
Although I don't think anyone on this board would recommend that approach.
 
Ok, I have the page working using a single static placeholder.
That'll do the job. 100% of the content is retrieved from a SQL
Server database and everything down to the text is generated
dynamcially. I'm just fighting with the session object now. :)

I appreciate your help. Thanks!
 
Glad to here you got it "working'.

If you give up fighting the Session, post another entry and you will surely
be helped...

bill
 
Back
Top