Button Click Event To Do 2 Different Things?

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 9052 Views - Last Post: 10 November 2010 - 05:32 PM Rate Topic: -----

#1 RandomlyKnighted   User is offline

  • D.I.C Lover
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,384
  • Joined: 14-January 10

Button Click Event To Do 2 Different Things?

Posted 08 November 2010 - 08:54 PM

Hey everyone, I have a button that when I click is set to change the text to something else. How do I set it up so that when I click it again then it will do something else and not repeat the same event that it did before. Here is the code I have so far:

protected void Button1_Click(object sender, EventArgs e)
        {
            Button1.Text = "Week 1";
            Button3.Text = "Week 3";
            Button4.Text = "Week 4";
            Button5.Text = "Week 5";
        }



Please point me in the right direction, don't just give me code. Thanks in advance for all help given.

Is This A Good Question/Topic? 0
  • +

Replies To: Button Click Event To Do 2 Different Things?

#2 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3207
  • View blog
  • Posts: 19,239
  • Joined: 14-September 07

Re: Button Click Event To Do 2 Different Things?

Posted 08 November 2010 - 08:57 PM

Easiest thing would be to have a flag:

//boolean flag as part of the form
protected void Button1_Click(object sender, EventArgs e)
{
     if(flag){
       //do something
       flag = false;
     }
     else{
      //do something else
      flag = true;
     }
}



Edited code


Alternatively a counter that increments on click and then use the modulo operator to determine behavior at certain click # intervals.

If you need more behavior it might be worth looking into delegates/events depending on what you're using to determine behavior aside from clicks.
Was This Post Helpful? 0
  • +
  • -

#3 RandomlyKnighted   User is offline

  • D.I.C Lover
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,384
  • Joined: 14-January 10

Re: Button Click Event To Do 2 Different Things?

Posted 08 November 2010 - 09:19 PM

So it reads the flag as a click?
Was This Post Helpful? 0
  • +
  • -

#4 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3207
  • View blog
  • Posts: 19,239
  • Joined: 14-September 07

Re: Button Click Event To Do 2 Different Things?

Posted 08 November 2010 - 09:21 PM

Whoops, forgot a part. You have to set the flag to its opposite (aka true/false) on each click to get "alternating" behavior:

//boolean flag as part of the form
protected void Button1_Click(object sender, EventArgs e)
{
     if(flag){
       //do something
       flag = false;
     }
     else{
      //do something else
      flag = true;
     }
}




The button fires this function when its clicked, you're just using a "flag" to change between behaviors on click.
Was This Post Helpful? 0
  • +
  • -

#5 RandomlyKnighted   User is offline

  • D.I.C Lover
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,384
  • Joined: 14-January 10

Re: Button Click Event To Do 2 Different Things?

Posted 08 November 2010 - 09:53 PM

Here is what I did:

protected void Button1_Click(object sender, EventArgs e)
        {
            bool flag = true;
            
            if(flag)
            {
                Button1.Text = "Week 1";
                Button3.Text = "Week 3";
                Button4.Text = "Week 4";
                Button5.Text = "Week 5";

                flag = false;
            }
            
            else
            {
                flag = true;
                Button1.Visible = false;
                Button2.Visible = false;
                Button3.Visible = false;
                Button4.Visible = false;
                Button5.Visible = false;
                Button6.Visible = false;
            }  
        }



But when I hit the button a 2nd time nothing happens. Did I forget something?
Was This Post Helpful? 0
  • +
  • -

#6 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3207
  • View blog
  • Posts: 19,239
  • Joined: 14-September 07

Re: Button Click Event To Do 2 Different Things?

Posted 08 November 2010 - 09:55 PM

The flag has to be declared outside the scope of the click method. I'd put it at the class (your form) level.
Was This Post Helpful? 0
  • +
  • -

#7 RandomlyKnighted   User is offline

  • D.I.C Lover
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,384
  • Joined: 14-January 10

Re: Button Click Event To Do 2 Different Things?

Posted 08 November 2010 - 10:18 PM

Am I doing something wrong?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AsiSS_Imagine_Cup
{
    public partial class Main : System.Web.UI.Page
    {

        bool flag = true;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            Button2.Visible = false;
            Button6.Visible = false;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {            
            if(flag)
            {
                Button2.Visible = true;
                Button6.Visible = true;
                
                Button1.Text = "Week 1";
                Button3.Text = "Week 3";
                Button4.Text = "Week 4";
                Button5.Text = "Week 5";

                flag = false;
            }
            
            else
            {
                Button1.Visible = false;
                Button2.Visible = false;
                Button3.Visible = false;
                Button4.Visible = false;
                Button5.Visible = false;
                Button6.Visible = false;

                flag = true;
            }  
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Button1.Text = "Week 1";
            Button3.Text = "Week 3";
            Button4.Text = "Week 4";
            Button5.Text = "Week 5";
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            Button1.Text = "Week 1";
            Button3.Text = "Week 3";
            Button4.Text = "Week 4";
            Button5.Text = "Week 5";
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            Button1.Text = "Week 1";
            Button3.Text = "Week 3";
            Button4.Text = "Week 4";
            Button5.Text = "Week 5";
        }



    }
}


Was This Post Helpful? 0
  • +
  • -

#8 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3207
  • View blog
  • Posts: 19,239
  • Joined: 14-September 07

Re: Button Click Event To Do 2 Different Things?

Posted 08 November 2010 - 10:20 PM

First button1 click: sets two buttons visible and sets some text
2nd button1 click: all buttons invisible
3rd button1 click: sets two buttons visible and sets some text
4th button1 click: all buttons invisible
.
.
.
n button1 clicks


Is that behavior different then your intention(s)?
Was This Post Helpful? 0
  • +
  • -

#9 Nakor   User is offline

  • Professional Lurker
  • member icon

Reputation: 448
  • View blog
  • Posts: 1,504
  • Joined: 28-April 09

Re: Button Click Event To Do 2 Different Things?

Posted 08 November 2010 - 10:37 PM

looks like it should work, what's it doing compared to what you want it to?
Was This Post Helpful? 0
  • +
  • -

#10 RandomlyKnighted   User is offline

  • D.I.C Lover
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,384
  • Joined: 14-January 10

Re: Button Click Event To Do 2 Different Things?

Posted 08 November 2010 - 10:42 PM

View PostKYA, on 08 November 2010 - 10:20 PM, said:

First button1 click: sets two buttons visible and sets some text
2nd button1 click: all buttons invisible
3rd button1 click: sets two buttons visible and sets some text
4th button1 click: all buttons invisible
.
.
.
n button1 clicks


Is that behavior different then your intention(s)?


Yes, it is different. My intention was to press Button1 and then it makes all buttons visible as well as changes the text. Then when you press the button again it will make all buttons invisible.

Right now, you press the button and it makes all visible and changes the text. But when you press it the 2nd time the pages reloads as if it did something but then nothing happens. It's like it repeats the first step.

This post has been edited by RandomlyKnighted: 08 November 2010 - 10:45 PM

Was This Post Helpful? 0
  • +
  • -

#11 Nakor   User is offline

  • Professional Lurker
  • member icon

Reputation: 448
  • View blog
  • Posts: 1,504
  • Joined: 28-April 09

Re: Button Click Event To Do 2 Different Things?

Posted 08 November 2010 - 11:02 PM

Maybe try this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AsiSS_Imagine_Cup
{
    public partial class Main : System.Web.UI.Page
    {

        bool flag;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            Button2.Visible = false;
            Button6.Visible = false;
            
            if (!IsPostBack) flag = true;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {            
            if(flag)
            {
                Button2.Visible = true;
                Button6.Visible = true;
                
                Button1.Text = "Week 1";
                Button3.Text = "Week 3";
                Button4.Text = "Week 4";
                Button5.Text = "Week 5";

                flag = false;
            }
            
            else
            {
                Button1.Visible = false;
                Button2.Visible = false;
                Button3.Visible = false;
                Button4.Visible = false;
                Button5.Visible = false;
                Button6.Visible = false;

                flag = true;
            }  
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Button1.Text = "Week 1";
            Button3.Text = "Week 3";
            Button4.Text = "Week 4";
            Button5.Text = "Week 5";
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            Button1.Text = "Week 1";
            Button3.Text = "Week 3";
            Button4.Text = "Week 4";
            Button5.Text = "Week 5";
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            Button1.Text = "Week 1";
            Button3.Text = "Week 3";
            Button4.Text = "Week 4";
            Button5.Text = "Week 5";
        }



    }
}




Was This Post Helpful? 0
  • +
  • -

#12 RandomlyKnighted   User is offline

  • D.I.C Lover
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,384
  • Joined: 14-January 10

Re: Button Click Event To Do 2 Different Things?

Posted 08 November 2010 - 11:13 PM

When I do that, it makes all the buttons invisible on first click.
Was This Post Helpful? 0
  • +
  • -

#13 Nakor   User is offline

  • Professional Lurker
  • member icon

Reputation: 448
  • View blog
  • Posts: 1,504
  • Joined: 28-April 09

Re: Button Click Event To Do 2 Different Things?

Posted 08 November 2010 - 11:32 PM

ok, you need to store the flag in a session so that it carries across on the postback, right now it is getting reset everytime.

protected void Page_Load(object sender, EventArgs e)
        {
            Button2.Visible = false;
            Button6.Visible = false;           
			if (!IsPostBack) Session["flag"] = false;
			
			System.Web.UI.ScriptManager.RegisterStartupScript(Button1,
			                                                  typeof(Button),
			                                                  "button",
			                                                  "alert('"+Session["flag"].ToString()+"');",
			                                                  true);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {            
            if(Convert.ToBoolean(Session["flag"]))
            {
                Button2.Visible = true;
                Button6.Visible = true;
                
                Button1.Text = "Week 1";
                Button3.Text = "Week 3";
                Button4.Text = "Week 4";
                Button5.Text = "Week 5";
				
				Session["flag"] = false;
            }            
            else
            {
                //Button1.Visible = false;
                Button2.Visible = false;
                Button3.Visible = false;
                Button4.Visible = false;
                Button5.Visible = false;
                Button6.Visible = false;
				
				Session["flag"] = true;
            }  
			
			//if (flag) flag = false;
			
			System.Web.UI.ScriptManager.RegisterStartupScript(Button1,
			                                                  typeof(Button),
			                                                  "button",
			                                                  "alert('"+Session["flag"].ToString()+"');",
			                                                  true);
        }



I added a couple of javascript alerts to show the values of the flag on each postback
Was This Post Helpful? 1
  • +
  • -

#14 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Button Click Event To Do 2 Different Things?

Posted 09 November 2010 - 05:46 AM

And this is why you need to post in the correct forum. As you can see, nobody knew that you were talking about ASP.Net, where form level variables do not store their state across postbacks. That's why 's code didn't work. If he would have known that you were posting about ASP.Net, then he would have given you the correct code.

Just because you are posting about a C# question, doesn't mean it should always be posted in the C# Forum. The technology you are using(ASP.Net in your case) does matter. As you can see, it changes the solution.
Was This Post Helpful? 0
  • +
  • -

#15 Sergio Tapia   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Button Click Event To Do 2 Different Things?

Posted 09 November 2010 - 05:52 AM

Isn't there a property for controls that you can select as true called AutopostBack? Wouldn't enabling that do the same?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2