Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

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: 0.0/5 (0 votes cast)