2

I have a bunch of LinkButtons on an asp.net page and need to set the visibility property of all the other LinkButtons on the page that have the same onclick attribute. I'm looking for a server side solution.

In the click handler I've gotten as far as listing the LinkButtons on the Page recursively but am stumped at how to tell if each LinkButton I find does or doesn't have a matching click handler.

The EventHandler property doesn't seem to contain any good info...

What is the best way to approach this?

foldinglettuce
  • 522
  • 10
  • 28

3 Answers3

1

You can give your linkbuttons a custom atttribute (for simplicity it can be the same as event handler name) e.g.

<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton_Click" tag="LinkButton_Click">LinkButton1</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton_Click" tag="LinkButton_Click">LinkButton2</asp:LinkButton>

Then in your server-side code you can simple compare the attributes

protected void LinkButton_Click(object sender, EventArgs e)
{
    // your recursive code retrieving current linkbutton 
    // ...
    if ((sender as LinkButton).Attributes["tag"] == currentLinkbutton.Attributes["tag"]) 
    {
       // do your magic
    }
}
Yuriy Galanter
  • 38,833
  • 15
  • 69
  • 136
0

Did you try linkbtn.Attributes["OnClick"]

voddy
  • 950
  • 1
  • 11
  • 21
  • After looping thru the keys for the Attributes collections for all the linkbuttons it doesn't look like "OnClick" is stored here... Thanks for the idea tho. – foldinglettuce Mar 25 '14 at 17:19
0

An ugly solution would be to give all of those buttons the same CommandName attribute. Then you can just search for all link buttons with that attribute. Otherwise I am not sure that there is a great way to track the event handler used by each LinkButton.

bsayegh
  • 990
  • 6
  • 17
  • Everything I've tried so far has been ugly... I'll look at this option. Thanks. – foldinglettuce Mar 25 '14 at 18:32
  • Further reading on how event handlers work lead me to believe this is the best I can do with the page as it currently exists without a major rewrite. So I'll accept this answer. This question helped: http://stackoverflow.com/questions/3212721/check-if-the-control-has-events-on-click-eventhandler – foldinglettuce Mar 25 '14 at 19:34