RegisterStartupScript, RegisterOnSubmitStatement
Dec14

RegisterStartupScript, RegisterOnSubmitStatement

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: Create the script as a string. 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”...

Read More
RegisterClientScriptBlock, RegisterClientScriptInclude
Dec14

RegisterClientScriptBlock, 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 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: Create the script as a string. 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">// <!&#91;CDATA&#91; alert('Hello World); // &#93;&#93;></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...

Read More
Tips and Tricks: C# Code Surround-with Snippets
Dec12

Tips and Tricks: C# Code Surround-with Snippets

We 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. Surround-with snippets are added around a selected block of code. How to use Surround-with snippets? Select the lines of code you want to surround with a snippet. class CodeSpread { void printname() { Console.WriteLine(“CodeSpread”); } }   Now Either press CTRL + K, CTRL + S OR select one of the option from “Surround With” in Intellisense. Here, if we select “if” snippet, then it will wrap the selected code snippet with the “if” statement. class CodeSpread { void printname() { if(true) { Console.WriteLine(“CodeSpread”); } } } Is Surround-with feature available for front end? Yes, it can be used in HTML markup also. Follow the same steps as above. With the help of this feature, you can surround the HTML markup with a snippet like “div” snippet, “form” snippet or more. Even in case of asp.net control, same steps can be used to surround the control with snippet like “Panel” snippet, “Repeater” snippet. Isn’t it...

Read More
Tips and Tricks: C# Code Expansion Snippets
Dec12

Tips 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 contains commonly-used code blocks such as property, try-finally or if-else blocks. We just have to type a few letters and then press tab twice [TAB TAB]. This is one of the useful ways to reduce our efforts of writing repetitive code again. Do we need any special configuration? No, as it is a feature available in Visual Studio. There are two kinds of code snippet: Expansion snippets, are added at a specified insertion point and may replace a snippet shortcut, Surround-with snippets are added around a selected block of code. We will talk about expansion snippets in this article and Surround-with snippets in the next article. Expansion Snippets: Try Catch Finally: In C#, the shortcut tryf is used to insert a try-finally block: class CodeSpread { tryf } type tryf and press TAB + TAB. The result will be class CodeSpread { try { } finally { } } Property: In C#, the shortcut prop is used to insert an automatically implemented properties block: class CodeSpread { prop } type prop and press TAB + TAB. The result will be class CodeSpread { public int MyProperty { get; set; } } Full Property: In C#, the shortcut propfull is used to insert a full property block: class CodeSpread { propfull } type propfull and press TAB + TAB. The result will be class CodeSpread { private int myVar; public int MyProperty { get { return myVar; } set { myVar = value; } } } Using: In C#, the shortcut using is used to insert using-statement block: class CodeSpread { static void Main() { using } } type using and press TAB + TAB. The result will be class CodeSpread { static void Main() { using (resource) { } } } Class: In C#, the shortcut class is used to insert class definition block: class type class and press TAB + TAB. The result will be class CodeSpread { } Constructor: In C#, the shortcut ctor is used to insert parameterless public constructor block: class CodeSpread { ctor } type ctor and press TAB + TAB. The result will be class CodeSpread { public CodeSpread () { } } do-while loop: In C#, the shortcut Do is used to insert do-while code block: class CodeSpread { static void Main() { do } } type do and press TAB + TAB. The result will be class CodeSpread { static void Main() { do { } while (true);...

Read More
IIRF.ini for URL rewriting
Jul22

IIRF.ini for URL rewriting

We often hear the terms like URL rewriting or friendly URLs. There are many techniques available to achieve it and we will discuss one of those today, IIRF. What is URL rewriting? Before we try to understand URL rewriting, we should first understand a term called SEO friendly URLs,Read more about it here. When Google and Yahoo spiders start indexing the page, they take various parameters in account for indexing the page and one of the most important parameter is URL. URL should be more user friendly and human readable. For example, A URL like codespread.com/article?p=123 is not human readable or user friendly, it should be rewritten something like codespread/article/know-url to get a better search ranking/indexing. Above explanation fulfils the need of URL rewriting. How can it be achieved? URL rewriting can be achieved by either with ISAPI filters at the IIS Web server level, or with either HTTP modules or HTTP handlers at the ASP.NET level. We are going to focus on ISAPI filter at the IIS web server level. We have a free and open source ISAPI filter for IIS URL rewriting from Ionic: IIRF: Ionic’s ISAPI Rewriting Filter. It installs as an ISAPI Filter on the IIS server. Note:IIS servers do not support URL rewriting by default. IIS7 servers support native component for URL rewriting. Refer this beautiful article to install ISAPI filter. http://wiki.gxtechnical.com/commwiki/servlet/hwiki?How+to+install+URL+Rewrite, In IIS7, URL rewrite will be seen as, In II6, IIRF.dll can be seen inside ISAPI filters property. Both option uses IIRF.dll for basic filter. After this, we just need to edit the configuration file ‘IIRF.ini’ to establish rules. What is IIRF.ini? ‘ini’ file is a simple text file with an extension of ‘Initialization’. It provides the settings for the sites which is the configuration information normally referred to as the rules. These rules describe how to rewrite or redirect URLs and headers. For each site,at least one IIRF.ini is necessary. It is required to locate the IIRF.ini file in the root physical path for each web site or web application. IIRF uses regular expression for rules like \d, \D, \w, \W, \s, \S. An example: This is a basic introduction to IIRF. There is more information and examples available at...

Read More
DI: Dependency Injection Summary
Apr30

DI: Dependency Injection Summary

Here is a summary of Dependency Injection implementation. This ppt is also available for download. Download (PPT, 941KB) You can also check the same ppt on our youtube...

Read More
Page 1 of 41234