Nannette Thacker ShiningStar.net

ASP.net Web Application Development

Sponsors

News

See all Blog Posts by Nannette.

Nannette Thacker, consultant and owner of Shining Star Services LLC, specializes in development of custom dynamic database driven web applications utilizing ASP.net technologies. Nannette has been developing ASP sites since 1997. Nannette has written numerous articles on web development techniques and tutorials.

Nannette is the owner and developer of ChristianSinglesDating.com.

 Subscribe in a reader




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.

May your dreams be in ASP.NET!

Nannette

Comments

Attributes.Add: Adding Javascript Click Events Programmatically in Code-Behind - Nannette Thacker ShiningStar.net said:

Pingback from  Attributes.Add: Adding Javascript Click Events Programmatically in Code-Behind - Nannette Thacker ShiningStar.net

# January 29, 2009 10:37 AM

m said:

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

# January 29, 2009 11:01 AM

tsantos said:

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.

# January 29, 2009 12:19 PM

nannette said:

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.

:)

# January 30, 2009 3:23 PM

David said:

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!

# February 9, 2009 12:46 PM

kansasfiddler said:

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

# March 5, 2009 1:22 PM

Jeff said:

Gute Arbeit hier! Gute Inhalte.

# March 6, 2009 7:55 PM

j said:

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..

# March 20, 2009 5:55 AM

Brad said:

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.

# August 14, 2009 3:28 PM

Chuck said:

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

# September 13, 2009 11:54 PM

Hasu said:

can anyone help me,

i am developing a website in asp.net

i have radio button list , when "yes" is selected submit button should enabled and when "no" is selected submit button should disable...........

thanks

# October 1, 2009 5:21 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)