Alex Mueller on Software and Technology 
Friday, October 26, 2007


In an ASPX page or an ASCX user control, we can name our JavaScript functions using a prefix, <%=ClientID%>, in order to mitigate function naming collisions. In some applications, there can be a number of JavaScript functions, and in situations where we are continually adding more functions, sometimes we run the risk of repeating their names.

Perhaps I am the last person to realize, but to make my function names unique to my control or page, I can prefix them with <%=ClientID%>, as seen below.

Within an ASPX page:
function <%=ClientID%>_TestMe()
{
  // do whatever
}

Witin an ASCX user control:
function <%=ClientID%>_TestMe()
{
  // do whatever
}

When I include the above ASCX control within my ASPX page, essentially having two functions with the same name at design time, they are both rendered to the page's source with different names, as seen below.

<html>
  <head>
    <script>
      function <%=ClientID%>_TestMe()
      {
        // do whatever
      }
    </script>
  </head>
  <body>
      <form id="form1" runat="server">
        <div>
            <uc:Grid id="gridView" runat="server"></uc:Grid>
        </div>
      </form>
  </body>
</html>

Page Source as it is Rendered:
function __Page_TestMe()
{
  // do whatever
}

function gridView_TestMe()
{
  // do whatever
}

In my user control or page, I can reference my function with something like this.
<input type="button" value="GO" onclick="<%=ClientID%>_TestMe()" />

Which gets rendered as this.
<input type="button" value="GO" onclick="gridView_TestMe();" />

This will help reduce naming collisions when referencing JavaScript libraries, but it will not eliminate all of them.

Friday, October 26, 2007 9:59:34 PM (Mountain Standard Time, UTC-07:00) | Comments [1] | Design | JavaScript#
Friday, October 26, 2007 11:46:24 PM (Mountain Standard Time, UTC-07:00)
Alex,

You may be able to get around this by using Page.RegisterClientScript (for 1.1) or ClientScript.RegisterClientScriptBlock (for 2.0).

These methods register script blocks with the page using a key/value combination. If the method has already been registered, it wont be output again.
Comments are closed.
MuellerDesigns.net
Search
On This Page
PowerShell Documentation
Automate Daily Tasks with PowerShell
SketchPath XPath Editor
Software Testing - Revisited
Architecting Buildings and Software
NBCOlympics.com with Silverlight
Marker Interfaces and C# Attributes
The Phone Screen
Working with ASP.NET MVC and MvcContrib
Thanks to BDD
Twitter
The Opposite of a Singleton?
Removing Duplicate Code in Functions
Add Vista Themes to Longhorn
Changing File Ownership In Vista and Longhorn
Most Popular
JavaScript ReplaceAll Functionality
What is polymorphism?
What is composition?
Sorting with IComparable and IComparer
Applying the Observer Pattern in ASP.NET
MVP in ASP.NET
What is abstraction?
What is encapsulation?
What is a class?
What is inheritance?
Authentication in ASP.NET
Calendar Controls
XPathNavigator.CheckValidity new for 2.0
SQL Server 2005 Connection Issues
Auto-attach to process '[####] aspnet_wp.exe' on m...
What is an object?
FreeTextBox
VMWare and VPC
An Example of Reflection using C#
Caring for the Team
Archive
Links
Categories
My Local Blog Map
Blogroll
About
Powered by:

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2009
MuellerDesigns.net

Sign In

Help Those In Need
The Hunger Site
Ronald McDonald House Charities (RMHC) of Western Washington & Alaska