Click here to Skip to main content
Licence 
First Posted 5 Mar 2003
Views 91,699
Bookmarked 33 times

Another Way to Register and Render ClientScripts in ASP.NET

By | 5 Mar 2003 | Article
ASP.NET provides an easy way to register and render clientside JavaScript code to the web browser. This is typically useful when we have embedded JavaScript in a user control and the script should not be duplicated along with the number of instances of the control.

Introduction

ASP.NET provides an elegant way to inject script from code-behind. With its facilities, we can have our own custom web controls (buttons etc.). Optionally, these custom web controls (.ascx or its associated code-behind) can have embedded JavaScript within them. Here we describe some gains and limitations that we may experience in the use of WebControls with JavaScript functions embedded in them.

The Scenario

Assume that we need a custom Reset button, which should not only just reset the form but optionally toggle some Label controls also. Optionally we would like to have different types of interfaces like button look-and-feel or hyperlink look-and-feel. Our natural choice is to give a UserControl with both Button and LinkButton, with optional properties to toggle visibility of one and hide the other. To prevent unnecessary roundtrips in the server, we may like to have this function embedded as a JavaScript. Now wherever we want to make use of this control, we use the syntax:

<%@ Register TagPrefix="STP" TagName="STP" SRC="./includes/rb.ascx" %>

Now we would be including this button like this:

  <STP:STP Id="Stp1" runat="server"/>

This solves the problem, right? But what happens if I want two buttons of this type in the same page.

  <STP:STP Id="Stp1" runat="server"/>
  <STP:STP Id="Stp2" runat="server"/>

Now the embedded JavaScript function in the .ascx that is Register TagPrefixed is duplicated twice in the rendered HTML page.

The Solution

In the .ascx page, enclose the embedded JavaScript within runat=SERVER DIVs. Your code should look like this:

<DIV runat="server" id="divScript">
  <Script Language="'JavaScript'">     
    function thisFunctionInUserControl()
    {
    }
  </Script>
</DIV>

Now in the code-behind of the UserControl, declare the DIV as:

   protected HtmlGenericControl divScript;

In the PageLoad method, just do the following:

string resetBtnScriptCb = divScript.InnerText;
divScript.Visible = false;
if (!Page.IsClientScriptBlockRegistered("resetbtnControlScript"))
{
   Page.RegisterClientScriptBlock ("resetbtnControlScript",resetBtnScriptCb);
}

Conclusion

This should ensure that only copy of the embedded function in the UserControl gets rendered onto the client browser. Of course, I admit that you can use StringBuilder to build the JavaScript in code-behind itself. Then there comes the problem of escaping single quotes/double quotes, adding newlines etc. Perhaps this method just involves toggling with a runat=SERVER DIV.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Vasudevan Deepak Kumar

Web Developer

India India

Member

Vasudevan Deepak Kumar is from Chennai, India who has been in the programming career since 1994, when he was 15 years old. He has his Bachelors of Engineering (in Computer Science and Engineering) from Vellore Engineering College (now VIT University). He also has a MBA in Systems from Alagappa University, Karaikudi, India.


He started his programming career with GWBasic and then in his college was involved in developing programs in Fortran, Cobol, C++. He has been developing in Microsoft technologies like ASP, SQLServer 2000. For sometime, he has also been with PHP and MySQL based development in one of his previous organizations. Now currently his focus is on Microsoft .NET World (ASP.NET, C# and Whidbey)


In his past-time, he listens to polite Carnatic Music.

Web Presence


Homepage

http://www.lavanyadeepak.tk/

Blogs



Technical




Gossips



Spiritual







Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionRollover submit button? Pinmembermacupryk10:28 4 Aug '03  
GeneralYet another way PinsussPhil Turtle21:34 11 Mar '03  
GeneralRe: Yet another way PinmemberDeepak Kumar Vasudevan18:18 12 Mar '03  
GeneralRe: Yet another way PinsussPhilip Turtle3:10 13 Mar '03  
GeneralRe: Yet another way PinmemberDeepak Kumar Vasudevan3:46 13 Mar '03  
GeneralRe: Yet another way PinsussPhil Turtle4:06 13 Mar '03  
GeneralWhoops - slight mistake in example code PinsussPhil Turtle3:24 14 Mar '03  
QuestionWhy??? PinmemberSteinar Herland21:38 5 Mar '03  
AnswerRe: Why??? PinmemberDeepak Kumar Vasudevan22:12 5 Mar '03  
GeneralRe: Why??? PinmemberSteinar Herland22:23 5 Mar '03  
GeneralRe: Why??? Pinmemberleppie6:37 6 Mar '03  
AnswerRe: Why??? Pinmemberleppie6:31 6 Mar '03  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 6 Mar 2003
Article Copyright 2003 by Vasudevan Deepak Kumar
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid