IsraGab
IsraGab

Reputation: 5175

How do I add an onclick Handler in asp.net

'm using the ad-Gallery code in my asp.net application. I've changed it a bit to be generic, because I wanted to download pictures from my database. It means that all the implementation is in the code behind (here's a piece of my C# code):

ul.Attributes.Add("class", "ad-thumb-list");
            tabs.Controls.Add(ul);
            int i = 1;
                foreach (Products item in _PicturesPage)
                {
                    ul.Controls.Add(li);
                    anchor.Attributes.Add("href", item.ImagePath);
                    image.Attributes.Add("src", "../Images/pictures/thumbs/"+i+".jpg");
                    image.Attributes.Add("title","A title for 12.jpg");
                    image.Attributes.Add("alt", "This is a nice, and incredibly descriptive, description of the image");
                    image.Attributes.Add("class","image3");
                    li.Controls.Add(anchor);
                    anchor.Controls.Add(image);
                    i++;
                }

I want to know if it's possible to intercept a click in one of the hyperlink ()? Thank you :)

Upvotes: 0

Views: 1696

Answers (1)

Mathias Becher
Mathias Becher

Reputation: 703

Just add a JavaScript event handler:

anchor.Attributes.Add("onclick", "YourJavaScriptFunction();");

Be sure to return false from the handler, if you want to prevent navigation to the link's href.

If anchor is an instance of HtmlAnchor you can use:

anchor.ServerClick += (sender, args) =>
    {
        // do stuff
    };

Upvotes: 1

Related Questions