Friday, October 16, 2009

How to get Browser height width on server side using ASP.Net Page Methods

You need to know how the ASP.Net Page methods work for this one. You may get some idea from this post as well.

I am not very good at manipulating CSS and Javascript to find out current browser height/width and set the control height/width accordingly so that the page looks right in all types of browsers. Hence I needed to get the Browser Height/Width on the server side and then use the appropriate CSS file to show page correctly.

1st - create Javascript functions to get the height/width:

function getBrowserWidth() {
if (window.innerWidth) {
return window.innerWidth;
else if (document.documentElement && document.documentElement.clientWidth != 0) {
return document.documentElement.clientWidth;
else if (document.body) {
return document.body.clientWidth;
return 0;
function getBrowserHeight() {
if (window.innerHeight) {
return window.innerHeight;
else if (document.documentElement && document.documentElement.clientHeight != 0) {
return document.documentElement.clientHeight;
else if (document.body) {
return document.body.clientHeight;
return 0;

2nd - create a WebMethod in your code behind. I have put this method in my base page so I can use it from any aspx page.

public static void SetResolution(int width, int height)
if (width <= 1024) width = 1024;
if (height <= 768) height = 768;
SiteSession.Current.BrowserWidth = width; //Session["BrowserWidth"] = width;
SiteSession.Current.BrowserHeight = height; //Session["BrowserHeight"] = height;

3rd - now the goal is to call this page method at the time of login or at the time when page resizes

PageMethods.SetResolution(getBrowserWidth(), getBrowserHeight(), voidFn, voidFn);

you can also create a function call it as:
window.resize = function () { PageMethods.SetResolution(getBrowserWidth(), getBrowserHeight(), voidFn, voidFn); }

Now, whenever the page loads and resizes, the browser height width will be stored into Session. You can use that while rendering next/other pages:

4th - in your master page for all pages create a Header as below:

<head id="Head1" runat="server">

And then add appropriate css file:

protected override void OnPreRender(EventArgs e)
HtmlLink myHtmlLink = new HtmlLink();
myHtmlLink.Href = string.Format("~/css/{0}", GetResFileName());
myHtmlLink.Attributes.Add("rel", "stylesheet");
myHtmlLink.Attributes.Add("type", "text/css");
// Add the HtmlLink to the Head section of the page.

public string GetResFileName()
string fileName = "1280x1024.css";
if (SiteSession.Current.BrowserHeight < 800)
fileName = "1024x768.css";
return fileName;

Happy Diwali

Here is President Obama wishing everyone Happy Diwali.

He notes return of "Ram" that means he is more open than our psuedo-secular indian media and the leftists.

Friday, October 2, 2009

Today Google's Logo is Gandhi for G. Great! and Thank you Google.

Thursday, October 1, 2009

How to use Session in ASP.Net

The traditional way of using Session in ASP.Net/C#/VB.Net has been to use Session["variablename"].

Disadvantages of using Session this ways is:
* You could have typo in the variable name and can get errors that takes time to figure out the reason.
* Session["x"] will return an object that you would have to convert to the type that you need
* You never know what are all the session variables used in the application and what is the size of session at any given time
* There is no intellisense to tell you if a Session variable already exists
* Different developers may use different names to store the same information in session

A better approach of using Session is to create a (semi)singleton class as following.

public class SiteSession
private object _lock = new object();

//do not allow creating new objects of this
private SiteSession()
{ ; }

public static SiteSession Current
if(HttpContext.Current.Session["SiteSession"] == null)
if(HttpContext.Current.Session["SiteSession"] == null)
HttpContext.Current.Session["SiteSession"] = new SiteSession();
return (HttpContext.Current.Session["SiteSession"] as SiteSession);

public string SessionVar1 { get; set; }
public int SessionVar2 { get; set;}

public class BasePage : System.UI.Web.Page
public SiteSession MySession { get { return SiteSession.Current; } }

Now, whenever you use SiteSession.Current or MySession in the pages, you will see the intellisense for your session variable. This now helps keep all session variables at one spot.
Also there is no typo any more. You don't have do the type conversion anymore.

You can also create a base-class for your user controls and add MySession property to that user control. So, you will be able to access the session variables easily from your user control as well.

In your static/page methods/web methods, you can use SiteSession.Current to access the session variables.