RegisterClientScriptBlock, RegisterClientScriptInclude

Javascript
Javascript

RegisterClientScriptBlock, RegisterClientScriptInclude

Welcome to CodeSpread!

“Current trends are more inclined towards the client model for a better UI experience where small pieces of client script are integrated into the application.”

These client scripts can be declaratively attached to the events on UI forms or can be added dynamically from server side.

How to add client script dynamically from server side?

We have ClientScriptManager class which contains various methods to add client scripts to a webpage.

RegisterClientScriptBlock:

This method adds a script block to the page object. This script block is rendered before any other element of the form is instantiated.  Because of this, it might not be able to reference all the elements on the page. This case might result in blockage of content rendering.

Steps:

  1. Create the script as a string.
  2. Pass it to one of the overloaded methods.

RegisterClientScriptBlock(Type type, String key, String script)

type signifies the type of the client script to register.

key signifies the key of the client script to register.

script signifies the client script literal to register.

RegisterClientScriptBlock(Type type, String key, String script Boolean addScriptTags)

addScriptTags signifies a Boolean value indicating whether to add script tags. If it is set to true,script tags[<script>] will be added automatically

Before adding a script with RegisterClientScriptBlock  we can actually check if it is already been added somewhere else in our code by using a method, IsClientScriptBlockRegistered. This method will check if script with a given key and type pair is already registered on the page.


ClientScriptManager csManager = Page.ClientScript;

Type vartype = this.GetType();

if (!csManager.IsClientScriptBlockRegistered(vartype ,"buttonClick"))

StringBuilder messagetxt = new StringBuilder();

messagetxt.Append("<script type="text/javascript">// <![CDATA[
alert('Hello World);
// ]]></script>");

csManager.RegisterClientScriptBlock(vartype, “buttonClick” , messagetxt.ToString() );

RegisterClientScriptInclude:

This method is very much similar to the RegisterClientScriptBlock method, but in this case, the script content is accessed by from a URL that points to a script file, an external .js file.

All the other points/steps will remain same as that of RegisterClientScriptBlock.

There are two overloaded methods:

RegisterClientScriptInclude(string key,string url)

RegisterClientScriptInclude(Type type, string key,string url)

url signifies the source of the script

Here, IsClientScriptIncludeRegistered is used to determine whether a client script is already included.

ClientScriptManager csManager = Page.ClientScript;

Type vartype = this.GetType();
if (!csManager. IsClientScriptIncludeRegistered (vartype ,"buttonClick"))

{

string scripturl= "~/external_dummmy.js";

csManager.RegisterClientScriptBlock(vartype , “buttonClick” , ResolveClientUrl(scripturl))

);

There are two more methods discussed in the next article.

Author: hershey

A passion for knowledge drives me to do programming, A passion for programming drives me to create something different, A passion for creation drives me to spread the knowledge.

Share This Post On

0 Comments

  1. nice superb explaination

    Post a Reply
  2. thanks for the tutorial
    I am having a problem.
    The feature “Create SQL server database” isn’t available , I can’t select it.
    Can you help, please ?

    Post a Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

More from CodeSpread:

  • RegisterStartupScript, RegisterOnSubmitStatementRegisterStartupScript, RegisterOnSubmitStatementWe have discussed RegisterClientScriptBlock, RegisterClientScriptInclude in our previous article here. There are two more methods to client script dynamically from server side. We will discuss thes...
  • UI: Jquery is Javascript LibraryUI: Jquery is Javascript LibraryJquery Basics: With the title itself, anybody can deduce that we are indirectly talking about javascript only. So, we can say that Javascipt is the guardian of Jquery. These are client end technol...
  • Styles of Source Code Comments in C#, SQL, XML, HTML, CSS, JavaScriptStyles of Source Code Comments in C#, SQL, XML, HTML, CSS, JavaScriptDevelopers take months to write beautiful pieces of code and reviewer gets only few hours or I say, minutes to provide suggestions and improvisations. Then it becomes a hard deal for a developer to...
  • jQuery: Effects/MethodsjQuery: Effects/MethodsIn our last article, jQuery Part 1 , we covered the basic understanding and now we will move to Methods or effects available in jQuery. Hide/Show/Toggle Methods The most used method Hide/Show. Fr...
  • Tips and Tricks: C# Code Surround-with SnippetsTips and Tricks: C# Code Surround-with SnippetsWe have discussed Code Snippets and one of the two types, expansion snippets in our previous article: Tips and Tricks: C# Code Expansion Snippets Now, we will discuss Surround-with snippets. ...
  • Nice reference for C# evolution Part 3Nice reference for C# evolution Part 3We completed till C# 3.0 introductions in last article and C# 2.0 in this article . We will forward our journey with C# 4.0 features. C#4.0 Late Binding (Dynamic) Late binding is always presumed...
  • Java script rendered content is not crawl able.Java script rendered content is not crawl able.There was a requirement to include 'ratings and reviews' plugin on the product pages of our website which is provided by a third party. We included a javascript library and some code as provided by...
  • Tips and Tricks: C# Code Expansion SnippetsTips and Tricks: C# Code Expansion Snippets“Ever felt like not writing the same code again? Yes! But what is the solution? Solution is Code Snippets.” What is a Code Snippet? Code snippets are small blocks of reusable code which contain...