Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > adding a textbox run time gives error

Reply
Thread Tools

adding a textbox run time gives error

 
 
Beatrix
Guest
Posts: n/a
 
      11-16-2007
Dear All,

I have checked many places but couldn't find the error in this script.
Error is:

"Control 'YourAnswer' of type 'TextBox' must be placed inside a form tag
with runat=server"



Code:

private void Load_Controls()
{
TextBox txtYourAnswer = new TextBox();
txtYourAnswer.ID = "YourAnswer";
txtYourAnswer.Text = "";
Controls.Add(txtYourAnswer);
}

if I add:
HtmlForm f =FindControl("form1");

and change last row to f.Controls.Add(txtYourAnswer);

I get:

Error 1 Cannot implicitly convert type 'System.Web.UI.Control' to
'System.Web.UI.HtmlControls.HtmlForm'. An explicit conversion exists (are you
missing a cast?) D:\Projects\QuizFolder\Quiz3\Q1.aspx.cs 33 21 D:\...\Quiz3\

Thank you for your help,
 
Reply With Quote
 
 
 
 
D.Dark
Guest
Posts: n/a
 
      11-16-2007
The FindControl method returns a Control object. Even if the control is a
Form it is still a Control object and not a Form. You have to convert it to a
Form like this:

HtmlForm f = (HtmlForm)FindControl("form1");


"Beatrix" wrote:

> Dear All,
>
> I have checked many places but couldn't find the error in this script.
> Error is:
>
> "Control 'YourAnswer' of type 'TextBox' must be placed inside a form tag
> with runat=server"
>
>
>
> Code:
>
> private void Load_Controls()
> {
> TextBox txtYourAnswer = new TextBox();
> txtYourAnswer.ID = "YourAnswer";
> txtYourAnswer.Text = "";
> Controls.Add(txtYourAnswer);
> }
>
> if I add:
> HtmlForm f =FindControl("form1");
>
> and change last row to f.Controls.Add(txtYourAnswer);
>
> I get:
>
> Error 1 Cannot implicitly convert type 'System.Web.UI.Control' to
> 'System.Web.UI.HtmlControls.HtmlForm'. An explicit conversion exists (are you
> missing a cast?) D:\Projects\QuizFolder\Quiz3\Q1.aspx.cs 33 21 D:\...\Quiz3\
>
> Thank you for your help,

 
Reply With Quote
 
 
 
 
Beatrix
Guest
Posts: n/a
 
      11-17-2007
Thankyou that worked. Can U please explain, why textbox is not working like
label? What is the meaning of this mess with htmlform and findcontrol?

If I put all my controls in this htmlform, like radiobuttons, etc it will
always work?

Thank you,
 
Reply With Quote
 
Riki
Guest
Posts: n/a
 
      11-19-2007
Beatrix wrote:
> Thankyou that worked. Can U please explain, why textbox is not
> working like label? What is the meaning of this mess with htmlform
> and findcontrol?
>
> If I put all my controls in this htmlform, like radiobuttons, etc it
> will always work?


A textbox has to be inside a form tag with the runat="server" attribute.
You can test this out by adding a textbox to your page in the editor, but
placing it outside the form tag.
You will get the same error as you did before.

The problem with your previous code was that you use:
Controls.Add(txtYourAnswer)
This is the same as:
Page.Controls.Add(txtYourAnswer)

This means that your textbox will be added AFTER all the other controls,
i.e. after the form tag (even after the closing body tag).
Which is causing the error.

When you use f.Controls.Add(myTextbox), it will be added after all the
controls INSIDE the form tag.

Hope this helps you understand.

--

Riki


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Run-time template list definition / Run-time variable type definition Pierre Yves C++ 2 01-10-2008 02:52 PM
create textbox at run time and insert data from textbox to database tanmaypatankar ASP .Net 0 12-18-2007 03:47 PM
FormView, empty TextBox gives error: Input string was not in a cor =?Utf-8?B?T3R0YXI=?= ASP .Net 6 05-24-2005 01:00 PM
FormView, empty TextBox gives error: Input string was not in a cor Neil Young ASP .Net 0 05-12-2005 09:29 PM
Adding Textbox to table at run time Ken Dippold ASP .Net 2 08-24-2004 09:43 PM



Advertisments