Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

ASP.NET: Append client side functions with Attributes.Add

From Wiki

Jump to: navigation, search

Summary: The Attributes.Add method can be used to add and append client side functions.

Need help with ASP.NET? Come and ask a question in our ASP.NET Forum

NOTE: This article applies only to HTML controls. ASP.NET controls do not exhibit this behaviour.

Say, for example, we had a HTML control that already had a javascript function attached to it's onblur event e.g.

  1. <input id="Text1" type="text" onblur="alert('Existing Function');" runat="server" />

If we used the Attributes.Add method to add another function, when we visit the page, the first function will have been replaced by our second. Try adding this and see the results:

  1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
  2.     Text1.Attributes.Add("onblur", "alert('New Function');")  
  3. End Sub

Notice how we only get an alert for the "New Function"? To combat this, we must append the exisiting functions as well e.g.

  1. Text1.Attributes.Add("onblur", "alert('New Function');" & Text1.Attributes.Item("onblur"))


This Hack is part of the ASP.NET Hacks collection

459 Rating: 2.3/5 (6 votes cast)