3 Replies - 2968 Views - Last Post: 25 February 2013 - 09:27 AM Rate Topic: -----

#1 Codey09  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 210
  • Joined: 20-August 09

Passing an ID value of an element to the On Click event

Posted 24 February 2013 - 01:33 PM

Hi Guys,

I'm currently working on a project where I have multiply panels on my page but only one is showing at any one time by the use of the Visible property.

What I'm trying to do is have 2 buttons on the page one for previous and one for next, so when the user clicks next it displays the next panel and hides the current one.

What I would like to happen is when the user clicks the button the event handler can tell which panel is currently showing. With this information I can then in the C# code behind display the correct panel based on the Id of the current visible panel.

Can you guys please point me in the right direction I have tried google but not getting the answers I need.

many Thanks

Codey

Is This A Good Question/Topic? 0
  • +

Replies To: Passing an ID value of an element to the On Click event

#2 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3738
  • View blog
  • Posts: 13,067
  • Joined: 12-December 12

Re: Passing an ID value of an element to the On Click event

Posted 24 February 2013 - 02:52 PM

In Javascript you would examine

document.getElementById("someid").style.visibility
// or 
document.getElementById("someid").style.display

but I don't use ASP.

This post has been edited by andrewsw: 24 February 2013 - 03:02 PM

Was This Post Helpful? 0
  • +
  • -

#3 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 445
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Passing an ID value of an element to the On Click event

Posted 24 February 2013 - 07:34 PM

You should just be able to check the Visible property in the Next or Previous click event.

Example:

protected void NextButton_Click(object sender, EventArgs e)
{
    if (Panel1.Visible)
    {
        Panel1.Visible = false;
        Panel2.Visible = true;
        Panel3.Visible = false;

        // Enable the previous button now since we are past the first panel
        PreviousButton.Enabled = true;
    }
    else if (Panel2.Visible)
    {
        Panel1.Visible = false;
        Panel2.Visible = false;
        Panel3.Visible = true;

        // Disable the NextButton because you are on the last panel
        NextButton.Enabled = false;
    }
}



You would pretty much do just the opposite for the PreviousButton click event. This is just one way you might could handle this scenario and it will most likely need to be tweaked for your specific purpose.
Was This Post Helpful? 1
  • +
  • -

#4 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 285
  • View blog
  • Posts: 1,503
  • Joined: 07-April 08

Re: Passing an ID value of an element to the On Click event

Posted 25 February 2013 - 09:27 AM

You can also get the ID by casting the Sender. I would say that the easiest way would be the way that Nakor said to do it, but if you have two different buttons that hit the same method, then using sender will make absolutely sure that it works the way you want it to.

So basically use a switch statement and pass in the ID from Sender casting it AS a button. (*Note: use as so that if Sender cannot be cast as a Button you will not get an object reference error when trying to call .ID)

Then have two void methods for navigating forward and backward and call them based off which button was pressed.


switch((Sender as Button).ID)
{
   Case Forward.ID:
.....
   Case Backward.ID:
.....
   default:
      break;
}


This post has been edited by rgfirefly24: 25 February 2013 - 09:29 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1