RegisterStartupScript, RegisterOnSubmitStatement

Javascript
Javascript

RegisterStartupScript, RegisterOnSubmitStatement

Welcome to CodeSpread!

We have discussed RegisterClientScriptBlock, RegisterClientScriptInclude in our previous article here. There are two more methods to client script dynamically from server side. We will discuss these methods: RegisterStartupScript, RegisterOnSubmitStatement.

RegisterStartupScript:

This method adds a script block into the page at the end of page i.e. just before the form closing tag. It has an advantage over other methods where if the script requires any elements to be available on the page then as the script is already loaded/rendered, the elements can be easily referenced and script will not throw any error.

Basic steps will remain same as previous methods.

Steps:

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

There are two overloaded methods:

RegisterStartupScript(Type type,string key,string script)
RegisterStartupScript(Type type,string key,string script, bool addScriptTags)

Before adding a script with RegisterStartupScript, we can actually check if it is already been added somewhere else in our code, IsStartupScriptRegistered . 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.IsStartupScriptRegistered (vartype ,"buttonClick"))
StringBuilder messagetxt = new StringBuilder();
messagetxt.Append(("<script type="text/javascript">alert('Hello World);</script> ");
csManager. RegisterStartupScript (vartype, “buttonClick” , messagetxt.ToString() );

RegisterOnSubmitStatement:

This method adds script that executes in response to the page’s onsubmit event. It provides an opportunity of validation before accepting the user’s submission.

RegisterOnSubmitStatement(Type type,string key,string script)

Basic steps will remain same as above.

Before adding a script with RegisterOnSubmitStatement,we can actually check if it is already been added somewhere else in our code, IsOnSubmitStatementRegistered.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 . IsOnSubmitStatementRegistered (vartype ,"buttonClick"))
StringBuilder messagetxt = new StringBuilder();
messagetxt.Append("<script type="text/javascript">alert('Hello World);</script> ");
csManager.RegisterOnSubmitStatement(vartype, “buttonClick” , messagetxt.ToString() );

“These methods can add an excellent flavor towards the client model for a better UI experience”

 

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:

  • RegisterClientScriptBlock, RegisterClientScriptIncludeRegisterClientScriptBlock, RegisterClientScriptInclude"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 declarat...
  • 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...
  • 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...
  • 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...
  • 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. ...
  • 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...
  • 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...