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. Advertising

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

    Dynamic Variables? OR Dynamic Controls

    =?Utf-8?B?VGVyb3M=?=, Aug 10, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    420
    Scott Allen
    Aug 10, 2004
  2. Replies:
    5
    Views:
    5,581
  3. Ronald
    Replies:
    2
    Views:
    837
    Robbe Morris [C# MVP]
    Jan 17, 2006
  4. Bob Rock
    Replies:
    1
    Views:
    626
    Cowboy \(Gregory A. Beamer\)
    Sep 17, 2006
  5. 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:
    459
    Eidolon
    May 4, 2004
Loading...

Share This Page