Acetamide Acetamide - 26 days ago 13
ASP.NET (C#) Question

How to change the image button back to the previous image when user click the button again in asp.net using C#

I am creating a seat reservation system, I have seating chart in my asp.net webpages. When user click on one of the seat, it will change to another image.

I have did that. However, I cannot figured out how to do if the user click the same seat again, the image will change to previous image meaning that user did not want to reserve that seat anymore.

Any method can do this?
I change my image upon the first click by code below:

protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
ImageButton1.ImageUrl = "seatreserved.png";
}


Thank you if anyone can help me with this.

Answer

You can use the same code as above, just wrap it in an if statement:

protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
    if(ImageButton1.ImageUrl != "seatreserved.png")
    {
        ImageButton1.ImageUrl = "seatreserved.png";
    }
    else
    {
        ImageButton1.ImageUrl = "seatNotreserved.png";
    }
}
Comments