Button.Visible = false ?

Discussion in 'ASP .Net' started by Marc Lefebvre, Nov 4, 2003.

  1. Why I was unable to hide the previous button in the following code ?


    private void ButtonClick(Object sender, System.EventArgs e)
    {
    Button button = (Button)sender;
    Button previousButton = (Button)Session["previousButton"];

    if( previousButton == null )
    {
    // keep the first button
    previousButton = button;
    Session["previousButton"] = previousButton;
    }
    else
    {
    // Hide both button
    button.Visible = false;
    previousButton.Visible = false; // E R R O R don't work
    previousButton = null;
    Session["previousButton"] = previousButton;
    }
    }

    Thank's

    Marc
    Marc Lefebvre, Nov 4, 2003
    #1
    1. Advertising

  2. Marc Lefebvre

    AW Guest

    You simply cannot store a button in the Session. Don't forget that when the
    page will be posted back, a NEW button will be created for any button
    control on the page; so if you save a button, and on the next postback set
    it to invisible, it will no be the button that exists on the page generated
    by the postback.

    To solve this problem, I suggest that you store the control's ID in the
    ViewState, and then use FindControl to find it.

    Your code would look like this:
    > private void ButtonClick(Object sender, System.EventArgs e)
    > {
    > Button button = (Button)sender;
    > Button previousButton =

    (Button)FindControl[(string)Session["previousButton"]];
    >
    > if( previousButton == null )
    > {
    > // keep the first button
    > previousButton = button;
    > Session["previousButton"] = previousButton.ID;
    > }
    > else
    > {
    > // Hide both button
    > button.Visible = false;
    > previousButton.Visible = false;
    > }
    > }

    --
    To reply, remove a "l" before the @ sign.

    Arnaud Weil - MCT, MCSD.Net, MCAD.Net
    AW, Nov 4, 2003
    #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. spamfurnace
    Replies:
    1
    Views:
    2,626
    Scott M.
    May 24, 2004
  2. tshad

    button visible/not visible

    tshad, Oct 26, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    4,429
    Brian Hoops
    Oct 28, 2004
  3. tshad

    Sections visible and not visible

    tshad, Jan 31, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    2,047
    tshad
    Jan 31, 2005
  4. Patrick Olurotimi Ige
    Replies:
    7
    Views:
    5,943
    Patrick Olurotimi Ige
    Jun 15, 2005
  5. tshad
    Replies:
    1
    Views:
    4,220
    tshad
    Oct 31, 2009
Loading...

Share This Page