Attributes.Add: Adding Javascript Click Events Programmatically in Code-Behind

I'm going to demonstrate how to add javascript events programmatically in codebehind using the Attributes.Add method. You may want to add your javascript attributes programmatically so that you can populate the values from a database.

For demonstration purposes, I'm going to add javascript click events to an image. Let's start out with this cute little script contributed by Paul Kurenkunnas that I found on Javascript.Internet.Com (click to see it in action), which enlarges and reduces an image size on click and double-click. This javascript is simple enough that anyone can use it to see Attributes.Add in action.

<img src="waterfall.jpg" width="150" height="200" onclick="this.src='waterfall.jpg';this.height=400;this.width=300" ondblclick="this.src='waterfall.jpg';this.height=200;this.width=150">

Next in our code in front, we place an image control:

<asp:Image ID="Image1" runat="server" />

In the codebehind, we want to programmatically setup everything else. Notice I hard-code in the dimensions and URL, but you could easily retrieve the values from a database table and use them instead. This demo is primarily for the purpose of showing how to use the Attributes.Add and ResolveClientUrl methods:

VB.net:

Image1.ImageUrl = "~/site/images/MyImage.jpg"
Dim imageSrc As String = ResolveClientUrl(Image1.ImageUrl)
Image1.Attributes.Add("onclick", "this.src='" & _
   imageSrc & "';this.height=600;this.width=400")
Image1.Attributes.Add("ondblclick", "this.src='" & _
   imageSrc & "';this.height=300;this.width=200")

C#:


Image1.ImageUrl = "~/site/images/MyImage.jpg"; 
string imageSrc = ResolveClientUrl(Image1.ImageUrl); 
Image1.Attributes.Add("onclick", "this.src='" + imageSrc +
   "';this.height=600;this.width=400"); 
Image1.Attributes.Add("ondblclick", "this.src='" + imageSrc +
   "';this.height=300;this.width=200"); 
 

The code generated within the browser is:

<img id="ctl00_MainBody_Image1"
onclick="this.src='../../../site/images/MyImage.jpg';
this.height=600;this.width=400"
ondblclick="this.src='../../../site/images/MyImage.jpg';
this.height=300;this.width=200"
src="../../../site/images/MyImage.jpg" style="border-width:0px;" /><br />

Notice the image path was originally setup with:

"~/site/images/MyImage.jpg"

We must use the ResolveClientUrl method to obtain a URL that can be used by the browser:

../../../site/images/MyImage.jpg

Just FYI, don't put a height and width for the image or the resizing won't work. 

The Attributes.Add method can be added to numerous controls, such as images, buttons, comboboxes, labels, textboxes, radio buttons, checkboxes and more.

[SIGNATURE]

19 Comments

  • I can't believe you took the time to blog about this ... lol ....

    this.height=600

    isn't even close to a decent implementation ... WOW

  • I don't wanna be mean or anything, but this isn't something worth to blog about since it's not a good implementation; I'd say it doesn't follow the best practices of client event handling as we have nowdays. Try to take a look in jQuery, Mootools or another JavaScript framework. It would definitly not hurt and clear out somethings.
    Now I do understand that in some cases it's necessary to do this on server side, but it's not a good idea in most cases.

  • Are you both objecting to the use of Attributes.Add to add javascript events, or are you objecting to the javascript that I chose for demo purposes of how to use Attributes.Add?

    I just grabbed this javascript because it was simple enough to demonstrate how to use Attributes.Add.

    I didn't see anyone looking at this in order to learn how to click and enlarge an image. I simply wanted to demonstrate the use of Attributes.Add.

    :)

  • I actually found this quite helpful. And it won't break the View State of your pages like jQuery is prone to do.

    It's nice to actually find a simple example that can be applied to any situation, as opposed to some of the examples you find online where you find a 4 page example, and all you really needed was that one line of code.

    I appreciate it, thank you!

  • It would be good to tell folks in what event to use the attribute.add...load doesn't seem to work.

  • Gute Arbeit hier! Gute Inhalte.

  • sounds like m and tsantos are cant program wont program; library monkeys.. think writing actual code offends such..

    good stuff needed the add event ty..

  • Who goes around posting comments on peoples blogs like this:???

    "I can't believe you took the time to blog about this ... lol ...."

    Get a life, loser. Some people might find this useful. Myself included. At least someone takes the time to post what they think is interesting instead of going around reading other peoples blogs and then complaining about them. Thanks nanette.

  • Thanks Nannette don't care what m and tsantos say, I just found this useful, Appreciate it.

  • Thanks...added the attribte.add in the Page_LoadComplete event and it worked perfect.

  • i'm gonna make my own journal

  • eh. interesting style!

  • Thanks a lot it helps me a lot
    Mostafa from Egypt

  • How do you capture the height and width of the image that has been clicked and enlarged?

  • Hey, I'm just learning and this helped. For the two bozo's who think you wasted your time. They can $*#&amp; %&amp;&amp;# as far as I'm concerned. &nbsp;:-)

  • I've been reading about imaging and this is perfect for what I need. Thanks for keeping it simple.

  • I really liked the article, and the very cool blog

  • This blog is concise ,perfect and really cool.This is what I am looking for.
    Thanks a lot.

  • Hi Nannete , thanks for the post , Im new in aspx and i found this very useful.

    One thing i need to know is how can i load the image for the the first time with the desired width and height (e.g. 100x100) if any predefine is not allowed ? Since the real image sizes will be loaded into the page exactly as it is from the first call.

    Thanks a lot, rockie from Indonesia

Comments have been disabled for this content.