Zeeshan Hirani

  • Global Namespace in C#

    By default if you do not specify a namespace for a class it will go to default namespace. What happens When you are in particular namespace that has the same class that is also available in default namespace. How do you access the class that is available in default namespace. With C# 2.0, global keyword was introduced which allows you to access class that is available in global namespace. Here is an example that illustrates the problem and how we use global to access class available in global namespace.

  • Callbacks in Asp.net Ajax

    Callbacks provide an ability to call a function passing in additional parameters that your function would need. In the past to achieve this objective, I have created global variables that can be referenced from any where in your code. . Instead using callbacks, you can pass in a context that has the information your function would need. In the example below, I am passing an array of numbers to a callback function which simply writes the array to a label. Here is the markup

  • Moving Divs on the page

    Today I had a need to move my progress bar control on top of any control that was being updated asynchronously. Therefore I had to reposition my progress bar when an update happened. This lead me to investigate asp.net Ajax client side library and see what api it provides me to move a div from one position to another on a page. Sure enough Sys.UI.DomElement provides a method called getbounds which returns the width,height, x and y coordinate of an element on a page. The DomElement object also provides static method called SetLocation that takes 3 parameters. First parameter is the element that you want to move. Second and third parameters specifies the x and y coordinate of the location where the element should be moved to. To demonstrate the usage I have created a simple page which has 2 buttons. First button simply returns width, height,x and y coordinate of an element on the page. Clicking the second button moves the div to new x and y location on the page. X and Y positions are obtained as user input from the textboxes on the page. Here is how the page looks like.

  • Prevent Enter Key using Asp.net Ajax

    Today at work, I had a requirement where I had to prevent user from pressing enter key when they are on a particular textbox because it would cause the page to postback which I wanted to prevent. Using asp.net Ajax, you can register for the keypress event. The key press event gets an argument of type Sys.UI.DomEvent. The DomEvent object has a property called charCode which tells you which key the user has pressed.You can compare the charCode value with Sys.UI.Key.enter to check if the user has pressed enter key. On confirmation that user has pressed enter key, I make use of method exposed on DomEvent class called preventDefault. PreventDefault basically prevents the default action from happening which in my case prevents the enter key from being executed. Here is the code that prevents enter key from being executed by trapping keypress event on textbox.

  • Debug Vs Release Version of JavaScript file

    When you are debugging application it is easier to work with debug version of js file because the code is indented and comments provide better readability. However when deploying it production debugs version would increase the load time of the page. ScriptManager makes it really easy to work with debug and release versions of JavaScript file by using its ScriptMode property. For instance if you are referencing a JavaScript file called Popup.js, in order to get debug and release support from ScriptManager, you need to have two versions of the the js file like Popup.js and Popup.debug.js and depending on the ScriptMode set on the ScriptManager it will pull the appropriate JavaScript file. Here is an example that shows how to reference debug and release version of a JavaScript file.

  • Getting Intellisense in External JavaScript files

    If you are having problems getting Intellisense to work in external JavaScript file, you need to add reference directive with 3 /// comments. If you are pointing to your copy of js file, use Path attribute otherwise use name attribute to reference any JavaScript files outputted by the script manager. Here is the syntax for referencing both kinds of JavaScript file.

  • Authentication and Profile Services in asp.net Ajax

    With .net 3.5 Microsoft introduced authentication and profile services which allows you to authenticate using client side libraries. You also have the ability to update values in your profile using asp.net profile services. To work with this example, you need to set to up Membership ship database by using aspnet_regsql command. Enable form based authentication by changing the authentication mode to Forms. If you have created asp.net 3.5 website project, you will see profile and authentication and role service registered in config section of web.config as follows.

  • Getting entire XML content

    Another hidden jewel in the new XML api is the ToString method. ToString method has been overwritten in XElement. In Dom W3C api if you were to do ToString, you would end up with object type. However ToString in XElement outputs the XML string itself. It is nice to be able to pull a certain fragment of XML once you have obtained the reference to a particular XElement. Here is a simple example that illustrates the nice feature.

  • Getting Node Value using XElement

    If you have used Xml api in previous versions of C#, you must have felt the pain to extract node value and cast it to the correct type. With C# 3.0 new xml api, getting Node value is as simple as casting the node to the right type. You can still use the old fashioned XmlConvert class and convert the value to the right type by passing in the Value property of the node to get to the node value. Here is an example that illustrates how easy it is to convert xml node value to the right type.

  • Using AsEnumerable Hint

    Recently I had a huge query that I wanted to execute on SQL server. However certain portion of query had to be executed in C# as linq to objects because SQL server cannot comprehend regular expression. In trying to figure how I can tell linq to SQL to not execute this portion of the query in SQL server, I discovered AsEnumerable operator. AsEnumerable operator is a hint that tells linq to SQL to execute the portion following it in memory. Here is an example of a query that gets the data for customers from SQL server but applied a regular expression filter in memory. It is not the best solution but illustrates how you can use these hints to drive what gets executed on SQL server.