Monday, 28 January 2013

Getting KeyCode Values in IE and other browsers

keycode = {

    getKeyCode : function(e) {
        var keycode = null;
      //for IE 
      if(window.event) {
            keycode = window.event.keyCode;
      //for other Browsers
     else if(e) {
            keycode = e.which;
        return keycode;

    getKeyCodeValue : function(keyCode, shiftKey) {
        shiftKey = shiftKey || false;
        var value = null;
        if(shiftKey === true) {
            value = this.modifiedByShift[keyCode];
        }else {
            value = this.keyCodeMap[keyCode];
        return value;

    getValueByEvent : function(e) {
        return this.getKeyCodeValue(this.getKeyCode(e), e.shiftKey);

    keyCodeMap : {
        8:"backspace", 9:"tab", 13:"return", 16:"shift", 17:"ctrl", 18:"alt", 19:"pausebreak", 20:"capslock", 27:"escape", 32:" ", 33:"pageup",
        34:"pagedown", 35:"end", 36:"home", 37:"left", 38:"up", 39:"right", 40:"down", 43:"+", 44:"printscreen", 45:"insert", 46:"delete",
        48:"0", 49:"1", 50:"2", 51:"3", 52:"4", 53:"5", 54:"6", 55:"7", 56:"8", 57:"9", 59:";",
        61:"=", 65:"a", 66:"b", 67:"c", 68:"d", 69:"e", 70:"f", 71:"g", 72:"h", 73:"i", 74:"j", 75:"k", 76:"l",
        77:"m", 78:"n", 79:"o", 80:"p", 81:"q", 82:"r", 83:"s", 84:"t", 85:"u", 86:"v", 87:"w", 88:"x", 89:"y", 90:"z",
        96:"0", 97:"1", 98:"2", 99:"3", 100:"4", 101:"5", 102:"6", 103:"7", 104:"8", 105:"9",
        106: "*", 107:"+", 109:"-", 110:".", 111: "/",
        112:"f1", 113:"f2", 114:"f3", 115:"f4", 116:"f5", 117:"f6", 118:"f7", 119:"f8", 120:"f9", 121:"f10", 122:"f11", 123:"f12",
        144:"numlock", 145:"scrolllock", 186:";", 187:"=", 188:",", 189:"-", 190:".", 191:"/", 192:"`", 219:"[", 220:"\\", 221:"]", 222:"'"

    modifiedByShift : {
        192:"~", 48:")", 49:"!", 50:"@", 51:"#", 52:"$", 53:"%", 54:"^", 55:"&", 56:"*", 57:"(", 109:"_", 61:"+",
        219:"{", 221:"}", 220:"|", 59:":", 222:"\"", 188:"<", 189:">", 191:"?",
        96:"insert", 97:"end", 98:"down", 99:"pagedown", 100:"left", 102:"right", 103:"home", 104:"up", 105:"pageup"


Thursday, 24 January 2013

Using embedded resources with .NET assemblies

When creating a custom task or other component that will plug into a web application it is often useful to embed the resources for that component in the assembly itself. This greatly simplifies deployment because you will only need to deploy the assembly instead of all the external resources like JS files or images.
Here are some simple instructions for using embedded resources.
  1. Add the resource to the project (Right-click the project in the solution explorer and click Add > Existing item).
  2. Set the Build Action property of the file to Embedded Resource. (Right-click the file in the solution explorer and click Properties).
  3. Add a WebResource attribute to AssemblyInfo.cs. Like this:

  4. [assembly: System.Web.UI.WebResource("esri_samples.esriZoomIn.png", "img/png")]
    In this example esri_samples is the default names namespace. To see the default namespace right-click your project in the solution explorer and click Properties. The default namespace can be found on the Application tab. esriZoomIn.png is the name of the file. img/png is the type. Below is another example for a javascript file.
    [assembly: System.Web.UI.WebResource("stl_samples.clipboard.js", "text/javascript")]
    In this example stl_samples is the default namespace and notice the type is now text/javascript.
  5. Use the ClientScript manager to use the resource.
    • For images the GetWebResourceUrl method will get you a URL to the image. For example the following code gets a url for the esriZoomIn image embedded above.
      string sZoomUrl = 
      Page.ClientScript.GetWebResourceUrl(this.GetType(), "esri_samples.esriZoomIn.png");
    • For JavaScript, you need to specify where you want the file included in the page. First you get a URL for the resource like the image example above, then you use the ScriptManager to register the client script. If you want the script to be executed when the page loads, use RegisterStartupScript. RegisterStartupScript will insert the script at the bottom of the form (just before the </form>), so it will be executed after the rest of the DOM elements on the page are loaded.
      If you want to include JavaScript that can be called from events on the page, use RegisterClientScriptInclude. This adds a script within the form where the control tag is located.
      With each approach the JavaScript is executed when the parser hits the tag. The important thing is that you don’t try to access, for instance, DOM elements from a script registered using RegisterClientScriptInclude, because those DOM elements might not be available. So these scripts are often used for declaring a set of script methods/classes for later use. Of course the major difference with RegisterClientScriptInclude is that it references a file where RegisterStartupScript contains a piece of JavaScript (it could just as well be a JavaScript file you reference, although this is not the intended way of using it). Each of these registration methods should be called in the PreRender event of the control or page. Below is an example of adding the clipboard.js file to the page.
      protected override void OnPreRender(EventArgs e) 
      //Embed the javascript in the page 
      string scriptLocation = 
      this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "stl_samples.clipboard.js"); 
      this.Page.ClientScript.RegisterClientScriptInclude("SharedLocationTaskScripts", scriptLocation); 
Some related reading you might find useful:

Wednesday, 23 January 2013

Find Control Inside GridView in Javascript

function SelectAll(id) {
            var grd = document.getElementById("<%= GridView1.ClientID %>");
            var cell;
            if (grd.rows.length > 0) {
                for (i = 1; i < grd.rows.length; i++) {

                    cell = grd.rows[i].cells[0];
                    for (j = 0; j < cell.childNodes.length; j++) {

                        if (cell.childNodes[j].type == "checkbox") {

                            cell.childNodes[j].checked = document.getElementById(id).checked;

Total Pageviews