RegisterStartupScript, RegisterOnSubmitStatement
Dec14

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: 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
Styles of Source Code Comments in C#, SQL, XML, HTML, CSS, JavaScript
Feb16

Styles of Source Code Comments in C#, SQL, XML, HTML, CSS, JavaScript

Developers 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 showcase their work. At this moment, comments proved to be a nice packaging which can enhance the visibility of code and also reduce maintenance issues in long run. There are different commenting styles and guides available on internet but not under a single package so this article is the desired wrapping to share a single stop solution of comment styles. Lets see my favorite C# first. Single Line Comments or Inline Comments These comments are most widely used and very useful in explaining the code,in case, there is a speed breaker in walk through that appears suddenly.These are written as //declaring variables int count=0; string name=string.empty; OR while writing a complex or confusing logic which requires step by step explanation. public void GetProductRatings { //Call third party rating service } Note: Do not continue to write your code in the same line which contains comments with ‘//’. That will comment your code also and you might spend some of your time in unnecessary debugging. Multiple Line Comments or Descriptive block I always say if something is available for use then it will be useful,just that frequency of use may vary. There is a big confusion between the use of single line comments and  multiple line comments. I would say both are good and requires some thought before inserting them in the code file. Lets say, we have  a situation where we need to share the full logic of the code, then it would become necessary to use multiple line comments or descriptive block. My personal recommendation is to use only one descriptive block at the top of each page which describes the logic of the page at single place.These comments are written as %*********************************************************************** Page Title: Employee Author: CodeSpread Decription: This page contains the following logic 1.Extract the information of Employee : GetEmployeeDetails() 2.Enlist all the Employee: GetAllEmployee() Created Date: Modified Date: ************************************************************************% Looks cool!! XML Comments feature The best time saver feature available to a developer. Lets see you wrote a function like //put your cursor here to include xml comments public employee GetEmployeeDetails(int employeeid) { //Implementation } Just type ‘/’ 3 times where you have put your cursor and voila, you have inserted xml comments with default fields. /// <summary> /// /// </summary> /// <param name="employeeid"></param> public employee GetEmployeeDetails(int employeeid) { //Implementation } Customize the comments as per the logic you have written /// <summary> /// Function to extract employee details /// </summary>...

Read More
jQuery: Effects/Methods
Dec09

jQuery: Effects/Methods

In 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. From the last tutorial, we already know, how to select an element of our page. Then, if we are successful, we just have call hide() or show() method as required. Do not forget to include the jQuery library file. <script type="text/&lt;span class=">// <!&#91;CDATA&#91; javascript</span>" src="/jquery/jquery-1.7.1.min.js"> // &#93;&#93;></script> <script type="text/&lt;span class=">// <!&#91;CDATA&#91; javascript</span>" language="javascript"> // &#93;&#93;></script> $(document).ready(function() { $("#btn").click(function () { $("div").show(); }); &nbsp; $(".btnclass").click(function () { $("p").hide(); }); }); &lt;/script&gt; Explanation: On click of a button which has ID “btn”, all the div elements in the page will not be visible to the user. On click of a button having CSS class “.btnclass”, all the paragraph elements in the page will be visible to the user. Note: Though there is no parameter mentioned for Hide/ Show methods in the above example ,still we have some liberty provided by jQuery in terms of optional parameters like, $(selector).hide(speed,callback); $(selector).show(speed,callback); Here, ‘speed’ is the string which can take predefined values like “slow”, “normal”, or “fast” or we can customized it by specifying the milliseconds running time of animation.For ex: $("p").hide(1000); And, ‘callback’ represents the function which will be executed, once the show/hide animation completes. In the below code, ‘event’ function is called as a callback and it will show all the div element of the page. function event (){ $(“div”).show(); } $("p").hide(1000, event); One more method related to show/hide is toggle(), lets see what it does, $("#btn").click(function(){ $("#label1").toggle(); }); Well, it is not difficult to deduce that on click of button with ID “btn”, label1 appearance toggles, if it is in visible state,it gets hidden and vice versa. Similar to Hide/Show methods, toggle also contains the same optional parameters “speed” and “callback”. Fade Methods I suppose, we all know the dictionary meaning of fade and that is the exact feature used by these method. So what are the animations we have related to Fade? fadeIn() : An invisible element would start appearing in fade mode. fadeOut() : A visible element would begin diminishing in fade mode. fadeToggle() : It performs both of the operations described above fadeTo() : element fades to a given opacity (value between 0 and 1). We will try to save time here by providing quick examples as the base would remain same. $("#btn").click(function(){ $("#label1").fadeIn(); $("#label2").fadeOut("slow"); $("#label3").fadeToggle(); $("#label4").fadeTo(“1000”,0.5); }); We mentioned two optional parameters “speed” and “callback” and their use. This two parameters are applicable to our fade methods also. There is an exception in case of...

Read More
UI: Jquery is Javascript Library
Dec08

UI: Jquery is Javascript Library

Jquery 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 technologies which are completely different from our server side technologies but now they have blend in. Lets try an example in Javascript first,we will try to set the background color of document in our page. For that we will declare a javascript function. <script type="text/javascript"> function setBackgroundColor(color) { document.body.style.backgroundColor=color; } ; </script> <button onclick="setBackgroundColor('&lt;span class=">000AAA')"&gt;Set Color</button> Well,try it and you will see the action. Next,to achieve the similar functionality we will use a Jquery. $(this).css('background-color', 'red'); Look at the difference,Now let me ask you a question which one is neat? and which one you will prefer? Ofcourse, we all know that jquery is much simple, neat and easy to handle. Well, how was this possible? Jquery is a library, so like all library contains books which we read, similar way Jquery contains a lot of common tasks which would have required a lot of javascript code to be written. These tasks can be called in a simple way through Jquery by just following some syntax which we will see later in this document. Why Jquery and not others? There are other frameworks also available so why jquery, because it is very much popular and is growing daily with lot of new tasks being added to the library. One of the major factor is that it is being used by almost all the big companies. What are the prerequisites? This is an important question as, if you know things like HTML CSS JavaScript Then half of the battle is won. How can i start? Go and download the latest jquery from jquery.com and add it into the folder where you are developing your application. There are two version of jquery available: Production Version- Download this version, if you are new to jquery. Development version If you do not want to download, then also there is a way to include jquery in your project. There is a copy of jquery available on Google and Microsoft network, hosted on their CDN. You have to include this code in your page to include the library in your project. <html> script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"> </script> </html> The ‘$’ syntax We have our library in place, we will start with declaring ‘$’. This is a shortcut for $(document).ready. Why it is required? This requirement is somewhat related to Javascript issues which were faced by developers.What really happened was that any javascript code written for a particular element of the page requires the element to be...

Read More
Page 1 of 212