HighTechTalks DotNet Forums  

adding a textbox run time gives error

ASP.net Building Controls microsoft.public.dotnet.framework.aspnet.buildingcontrols


Discuss adding a textbox run time gives error in the ASP.net Building Controls forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Beatrix
 
Posts: n/a

Default adding a textbox run time gives error - 11-16-2007 , 04:57 AM






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
  #2  
Old   
D.Dark
 
Posts: n/a

Default RE: adding a textbox run time gives error - 11-16-2007 , 09:52 AM






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:

Quote:
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
  #3  
Old   
Beatrix
 
Posts: n/a

Default RE: adding a textbox run time gives error - 11-17-2007 , 02:20 AM



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
  #4  
Old   
Riki
 
Posts: n/a

Default Re: adding a textbox run time gives error - 11-19-2007 , 07:05 AM



Beatrix wrote:
Quote:
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 Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.