adding a textbox run time gives error

Discussion in 'ASP .Net Building Controls' started by Beatrix, Nov 16, 2007.

  1. Beatrix

    Beatrix Guest

    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,
     
    Beatrix, Nov 16, 2007
    #1
    1. Advertising

  2. Beatrix

    D.Dark Guest

    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,
     
    D.Dark, Nov 16, 2007
    #2
    1. Advertising

  3. Beatrix

    Beatrix Guest

    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,
     
    Beatrix, Nov 17, 2007
    #3
  4. Beatrix

    Riki Guest

    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
     
    Riki, Nov 19, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken Dippold

    Adding Textbox to table at run time

    Ken Dippold, Aug 24, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    5,748
    entdevguy
    Aug 24, 2004
  2. =?Utf-8?B?T3R0YXI=?=
    Replies:
    6
    Views:
    8,085
    =?Utf-8?B?TW9uZ283NDc=?=
    May 24, 2005
  3. Neil Young
    Replies:
    0
    Views:
    1,865
    Neil Young
    May 12, 2005
  4. tanmaypatankar
    Replies:
    0
    Views:
    1,085
    tanmaypatankar
    Dec 18, 2007
  5. Pierre Yves
    Replies:
    2
    Views:
    491
    Pierre Yves
    Jan 10, 2008
Loading...

Share This Page