简体   繁体   中英

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 :)

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
    };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM