Dynamic ASP.NET controls??

Discussion in 'ASP .Net' started by ªL©ú©v, Feb 17, 2004.

  1. ªL©ú©v

    ªL©ú©v Guest

    Dear all:
    Can I use the following code to generate controls dynamically? But put
    it in a form having attribute "runat=server"??
    If I use the code below, it will cause some error because of myBtn
    doesn't have "runat=server".

    System.Web.UI.WebControls.Button myBtn = new Button();
    myBtn.ID = "dynaBtn";
    myBtn.Text = "dynaBtn";
    myBtn.Click += new System.EventHandler(this.ClickEventHandler);
    this.Controls.Add(myBtn);

    If this code can work exactly, how to specify "runat=server" to myBtn?

    All code above is written by C# and in code-behind.

    Thanks.

    Andrew Feb 17,2004
     
    ªL©ú©v, Feb 17, 2004
    #1
    1. Advertisements

  2. ªL©ú©v

    Jure Spik Guest

    Andrew, If you are creating button serverside (as you do in your example)
    you do not need to set runat=server as it is implied. I think your error is
    that button must be in a form with runat=server, and you are adding it to
    the Page itself if that is the case you must do:
    in html:
    <form runat='server' id='myForm'></form>

    in codebehind:
    myForm.Controls.Add(myBtn)


    Good luck, Jure
    "ªL©ú©v" <> wrote in message
    news:...
    > Dear all:
    > Can I use the following code to generate controls dynamically? But put
    > it in a form having attribute "runat=server"??
    > If I use the code below, it will cause some error because of myBtn
    > doesn't have "runat=server".
    >
    > System.Web.UI.WebControls.Button myBtn = new Button();
    > myBtn.ID = "dynaBtn";
    > myBtn.Text = "dynaBtn";
    > myBtn.Click += new System.EventHandler(this.ClickEventHandler);
    > this.Controls.Add(myBtn);
    >
    > If this code can work exactly, how to specify "runat=server" to myBtn?
    >
    > All code above is written by C# and in code-behind.
    >
    > Thanks.
    >
    > Andrew Feb 17,2004
    >
    >
     
    Jure Spik, Feb 17, 2004
    #2
    1. Advertisements

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. =?Utf-8?B?VGVyb3M=?=

    Dynamic Variables? OR Dynamic Controls

    =?Utf-8?B?VGVyb3M=?=, Aug 10, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    591
    Scott Allen
    Aug 10, 2004
  2. Replies:
    5
    Views:
    5,807
  3. Ronald
    Replies:
    2
    Views:
    1,061
    Robbe Morris [C# MVP]
    Jan 17, 2006
  4. Bob Rock
    Replies:
    1
    Views:
    896
    Cowboy \(Gregory A. Beamer\)
    Sep 17, 2006
  5. =?Utf-8?B?U3RldmUgRnVuaw==?=

    Dynamic Wizard with Dynamic Validation Controls

    =?Utf-8?B?U3RldmUgRnVuaw==?=, Oct 13, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    561
    =?Utf-8?B?U3RldmUgRnVuaw==?=
    Oct 13, 2006
  6. Max2006

    ASP.NET MVS and ASP.NET Dynamic Data

    Max2006, Jul 15, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    650
    Steven Cheng [MSFT]
    Jul 16, 2008
  7. opius
    Replies:
    0
    Views:
    1,340
    opius
    Jun 17, 2011
  8. Denny Smolinski via .NET 247

    Dynamic Controls, Placeholder, Retrieve Information from Dynamic Controls

    Denny Smolinski via .NET 247, May 4, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    663
    Eidolon
    May 4, 2004
Loading...