kelly
kelly

Reputation: 117

Unable to click on any button after code has be written in code behind

I implemented several buttons in my webpage as i am doing a booking system. I managed to restrict the user click on buttons in my webpage but my problem here is that user now cannot even select a button as when they try to click on the first button, my alert message will pop up asking them to only select one button. How do i allow users to select only one button and when they try to select another one, my alert message will come into usage. I suspect that is my count that is causing the problem.

here is my .cs code:

protected void Button1_Click(object sender, EventArgs e)
{
    int counter = 0;
    if (counter > 1)
    {
        Button1.Text = "Selected";
        Button1.BackColor = System.Drawing.Color.DarkGreen;
        Button2.Text = "Selected";
        Button2.BackColor = System.Drawing.Color.DarkGreen;
        startingTime.Text = "9AM";
        endingTime.Text = "11AM";
    }

    else
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Please select one slot only');", true);
    }
}
protected void Button2_Click(object sender, EventArgs e)
{
    int counter = 1;
    if (counter > 0)
    {
        Button2.Text = "Selected";
        Button2.BackColor = System.Drawing.Color.DarkGreen;
        Button3.Text = "Selected";
        Button3.BackColor = System.Drawing.Color.DarkGreen;
        startingTime.Text = "10AM";
        endingTime.Text = "12PM";

    }

    else
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Please select one slot only');", true);
    }
}
protected void Button3_Click(object sender, EventArgs e)
{
    int counter = 1;
    if (counter > 0)
    {
        Button3.Text = "Selected";
        Button3.BackColor = System.Drawing.Color.DarkGreen;
        startingTime.Text = "11AM";
        endingTime.Text = "1PM";

    }

    else
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Please select one slot only');", true);
    }
}

Upvotes: 0

Views: 283

Answers (3)

Aghilas Yakoub
Aghilas Yakoub

Reputation: 28970

you must try with this example for Button1

//Save your counter in Viewstate or InputHidden in order to persist

public int Counter 
{
    get
    {
        int s = (int)ViewState["Counter"];
        return (s == null) ? 0 : s;
    }
    set
    {
        ViewState["Counter"] = value;
    }
}


protected void Button1_Click(object sender, EventArgs e)
{
    Counter = Counter + 1;

    if (Counter > 1)
    {
        Button1.Text = "Selected";
        Button1.BackColor = System.Drawing.Color.DarkGreen;
        Button2.Text = "Selected";
        Button2.BackColor = System.Drawing.Color.DarkGreen;
        startingTime.Text = "9AM";
        endingTime.Text = "11AM";
    }

    else
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Please select one slot only');", true);
    }
}

Upvotes: 0

Ionică Bizău
Ionică Bizău

Reputation: 113335

Declare int counter = 0 as global variable.

Upvotes: 1

Botz3000
Botz3000

Reputation: 39600

This will always evaluate to false, so you always get into the else block:

int counter = 0;
if (counter > 1)

You should change counter after your code executed, also it should be a field of your class (otherwise any changes to it get lost as currently counter is gone once the method exits).

You could also use Button.Enabled to enable/disable buttons.

Upvotes: 3

Related Questions