Write web controls out to a page and associating event handlers

Discussion in 'ASP .Net' started by =?Utf-8?B?Tm92aWNl?=, Sep 23, 2004.

  1. Hey all, I realize this must be a fairly common requirement in ASP.NET but I
    have yet to come across the right way to do it.

    Basically, right now I have a single asp:label on my webpage and I am
    writing out to the Text property of that Label object.

    However, now I would like to write a control like a button to that Text and
    associate an event handler with that button.

    How should I do that - or am I going about this in the wrong way - i.e.
    should I be creating my own custom web control?

    Anyway, here is my attempt at writing an asp:button out to the page and
    associating an event handler with it:
    StringBuilder sb = new StringBuilder();
    StringWriter sw = new StringWriter(sb);
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    Button button = new Button ();
    button.Click += new EventHandler(nextButtonClick);
    htw.Write(button.);
    htw.Flush();
    MyLabel.Text += sb.ToString();

    Thanks for any help,
    Novice
     
    =?Utf-8?B?Tm92aWNl?=, Sep 23, 2004
    #1
    1. Advertising

  2. "Novice" <6tc1ATqlinkDOTqueensuDOTca> wrote in message
    news:...
    > Hey all, I realize this must be a fairly common requirement in ASP.NET but
    > I
    > have yet to come across the right way to do it.
    >
    > Basically, right now I have a single asp:label on my webpage and I am
    > writing out to the Text property of that Label object.
    >
    > However, now I would like to write a control like a button to that Text
    > and
    > associate an event handler with that button.
    >
    > How should I do that - or am I going about this in the wrong way - i.e.
    > should I be creating my own custom web control?


    I strongly recommend that you use a property named "Text" for text and
    nothing else. Even if something else (like HTML) works today, I'd worry that
    the authors of the control might forget that "Text" is not juts used for
    Text, and may break your code by making the control work according to their
    assumptions...

    If you want to dynamically display one of several controls (like your button
    example), there are several ways to do this. The simplest is to place all of
    the possible controls on the page with Visible set to false. Then, at
    run-time, change the Visible property of the control you want to see to
    true. The overhead of this isn't as high as it sounds, since a control with
    the Visible property clear will not render any HTML.

    Another way is to put a Panel control on the page, and at run-time, add the
    desired control (or controls) to the Panel:

    Button myButton = new Button();
    myButton.Text = "Button";
    myPanel.Controls.Add(myButton);

    ---
    John Saunders
     
    John Saunders, Sep 23, 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. News
    Replies:
    4
    Views:
    406
    Eliyahu Goldin
    Jul 20, 2004
  2. JWL
    Replies:
    1
    Views:
    322
    Benjamin Niemann
    Oct 31, 2006
  3. Dica
    Replies:
    1
    Views:
    407
    bruce barker
    Jun 20, 2007
  4. Lucas Tam

    RaisePostBackEvent and Web Controls Event Handlers

    Lucas Tam, Aug 31, 2003, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    431
    Teemu Keiski
    Aug 31, 2003
  5. Pekka Karjalainen

    [Beginner] Associating event handlers with elements

    Pekka Karjalainen, Aug 9, 2005, in forum: Javascript
    Replies:
    3
    Views:
    113
    Pekka Karjalainen
    Aug 10, 2005
Loading...

Share This Page