Saturday, April 17, 2010

ASP.NET Enable Session in Web Service Methods

Ever tried accessing session variables in Web Service methods? It won't work unless you add a little property in the WebMethod header of the functions.

For example, the basic hello world:

[WebMethod(EnableSession=true)]
public void HelloWorld()
{
    ...
}

Just add the statement EnableSession=true and you'll be able to access session variables found normally in aspx pages like the following sample.

string userid = Session["userid"].ToString();

Thursday, April 15, 2010

ASP.NET C# Set Value to Password Textbox in Code Behind

If ever you've tried setting value in code-behind to a textbox with TextMode property set to "Password", you would notice it doesn't display the masked text on the page. This is simply the way it is for security purposes, but there is a workaround so you can show the user there is actually text in the textbox.


Here is the snippet:


txt1.Text = "sample_password";


if (txt1.TextMode == TextBoxMode.Password) 
{
    txt1.Attributes.Add("value", txt1.Text);
}


Just set the same text value to the javascript attribute "value" and you're done.

Tuesday, April 13, 2010

Javascript OnLoad Page Resize and Move to Center

Here's a snippet for loading a web page with set width and height and moving it at the center of the screen.


window.onload = function() {
    var width = 800;
    var height = 600;
    window.resizeTo(width, height);
    window.moveTo(((screen.width - width) / 2), ((screen.height - height) / 2));
}


For this sample we resize the window to 800 by 600 and then placed it at the center of the screen by taking the screen width and height (current monitor resolution) then subtracting it with the set width and height (800 and 600 respectively) and finally diving it by 2. Now, simply place this script on the head part of the page and that's it.

Tuesday, April 6, 2010

Disable Refresh in Webpage Using Javascript (F5 & Ctrl + R)

Okay here's another script for disabling refresh (F5 & Ctrl + R) in your web pages.


document.onkeydown = function() {    
    switch (event.keyCode) { 
        case 116 : //F5 button
            event.returnValue = false;
            event.keyCode = 0;
            return false
        case 82 : //R button
            if (event.ctrlKey) { 
                event.returnValue = false
                event.keyCode = 0;  
                return false
            } 
    }
}

Same as before, you can add this to your main javascript file and place it in the head part of your page.

It works by adding a function to your page's onkeydown event. Then we simply get which button was pressed by using event.Keycode (116 for F5 and 82 for R). Also, notice the "if" statement for R, it simply checks if the control button was pressed at the same time as R by checking event.ctrlKey

But there's a limitation to handling page refreshes with this code. You simply cannot control the refresh button specific to your browsers' toolbar. 



So the only way to really prevent users to refresh the page is by using this code and at the same time opening your page at a new window without the toolbar. 

Check http://codingresource.blogspot.com/2010/01/javascript-windowopen-in-net.html for more info on how to do that.

Disable Right-Click in Webpage Using Javascript

Here's a simple javascript snippet you can use to disable right-click in your web pages:



document.oncontextmenu = function() { 
    alert('Right-click is disabled.');
    return false
}


Just place the above code anywhere in your main javascript file or in the head part of the main page. You can also remove or change the alert if you don't want to notify the user.