Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » Samples » Downloads
 
Add your own
alternative version
Go to top

Automatic style changes of ASP.NET controls as per client's screen resolution

, 13 Apr 2006
How to automatically send a resolution optimized markup of a web page to the client PC.
DynamicScreens_src.zip
DynamicScreens
1024.jpg
1280.jpg
800.jpg
bin
DynamicScreens.dll
DynamicScreens.csproj.webinfo
Global.asax
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace DynamicScreens
{
	/// <summary>
	/// Summary description for WebForm1.
	/// </summary>
	public class WebForm1 : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.TextBox TextBox1;
		protected System.Web.UI.WebControls.Label Label1;
		protected System.Web.UI.WebControls.CheckBoxList CheckBoxList1;
		protected System.Web.UI.WebControls.Image Image1;
		protected System.Web.UI.HtmlControls.HtmlInputHidden ServerResolution;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			//On initial page request, we will set 1024 resolution as default
			if (! Page.IsPostBack)
			{
				ServerResolution.Value="1280Res";				
			}		
			Page.DataBind();
		}

		
		//*** Variables Refference
		
		//'ServerResolution.Value' holds the value of resolution setting on server (i.e. "1280Res" for representing 1280 Resolution). It is used to compare on client side in javascript code.
		//'Request.Form["ClientResolution"] holds the value of resolution setting on client's PC (i.e. "1280Res" for representing 1280 Resolution). It is used to compare on server side  in CodeBehind. if met then dont send server the request to change the resolution vice versa  
		
		//***


		//this property (TB for representing TextBox control :) ) is used for simple databinding of css value to the TextBox control property 
		public string TB
		{
			get
			{
				 
				//if page is first time requested, then set the default resolution setting as 1280x1024 
				if (! Page.IsPostBack )
				{
					//Set the Html Hidden Input Control Value For Reffering on Client Through JavaScript
					ServerResolution.Value="1280Res";
					//Returning CSS class name according to client's resolution for TextBox control for setting its 'CssClass' property which is set through simple databinding
					return "textbox1280";					
				}
				else if( Request.Form["ClientResolution"] == "1280Res")
				{
					//Set the Html Hidden Input Control Value For Reffering on Client Through JavaScript
					ServerResolution.Value="1280Res";
					//Returning CSS class name according to client's resolution for TextBox control for setting its 'CssClass' property which is set through simple databinding
					return "textbox1280";
				}
				else if( Request.Form["ClientResolution"] == "1024Res")
				{
					//Set the Html Hidden Input Control Value For Reffering on Client Through JavaScript
					ServerResolution.Value="1024Res";
					//Returning CSS class name according to client's resolution for TextBox control for setting its 'CssClass' property which is set through simple databinding
					return "textbox1024";
				}
				else if( Request.Form["ClientResolution"] == "800Res")
				{
					//Set the Html Hidden Input Control Value For Reffering on Client Through JavaScript
					ServerResolution.Value="800Res";
					//Returning CSS class name according to client's resolution for TextBox control for setting its 'CssClass' property which is set through simple databinding
					return "textbox800";
				}
				else
				{					
					//if client resolution is not detected yet, then set default 1280x1024 resolution
					return "textbox1280";
				}
			}
		}

		//this property (IMG for representing Image control :) ) is used for simple databinding of css value to the TextBox control property 
		public string IMG
		{
			get
			{
				 
				//if page is first time requested, then set the default resolution setting as 1280x1024 
				if (! Page.IsPostBack )
				{
					//Set the Html Hidden Input Control Value For Reffering on Client Through JavaScript
					ServerResolution.Value="1280Res";
					//Returning CSS class name according to client's resolution for TextBox control for setting its 'CssClass' property which is set through simple databinding
					return "image1280";					
				}
				else if( Request.Form["ClientResolution"] == "1280Res")
				{
					//Set the Html Hidden Input Control Value For Reffering on Client Through JavaScript
					ServerResolution.Value="1280Res";
					//Returning CSS class name according to client's resolution for TextBox control for setting its 'CssClass' property which is set through simple databinding
					return "image1280";
				}
				else if( Request.Form["ClientResolution"] == "1024Res")
				{
					//Set the Html Hidden Input Control Value For Reffering on Client Through JavaScript
					ServerResolution.Value="1024Res";
					//Returning CSS class name according to client's resolution for TextBox control for setting its 'CssClass' property which is set through simple databinding
					return "image1024";
				}
				else if( Request.Form["ClientResolution"] == "800Res")
				{
					//Set the Html Hidden Input Control Value For Reffering on Client Through JavaScript
					ServerResolution.Value="800Res";
					//Returning CSS class name according to client's resolution for TextBox control for setting its 'CssClass' property which is set through simple databinding
					return "image800";
				}
				else
				{					
					//if client resolution is not detected yet, then set default 1280x1024 resolution
					return "image1280";
				}
			}
		}

		//this property (BT for representing Button control :) ) is used for simple databinding of css value to the Button control property
		public string BT
		{
			get
			{
				
				//if page is first time requested, then set the default resolution setting as 1280x1024
				if (! Page.IsPostBack )
				{
					//Set the Html Hidden Input Control Value For Reffering on Client Through JavaScript
					ServerResolution.Value="1280Res";
					//Returning CSS class name according to client's resolution for Button control for setting its 'CssClass' property which is set through simple databinding
					return "button1280";
					
				}
				else if( Request.Form["ClientResolution"] == "1280Res")
				{
					//Set the Html Hidden Input Control Value For Reffering on Client Through JavaScript
					ServerResolution.Value="1280Res";
					//Returning CSS class name according to client's resolution for Button control for setting its 'CssClass' property which is set through simple databinding
					return "button1280";
				}
				else if( Request.Form["ClientResolution"] == "1024Res")
				{
					//Set the Html Hidden Input Control Value For Reffering on Client Through JavaScript
					ServerResolution.Value="1024Res";
					//Returning CSS class name according to client's resolution for Button control for setting its 'CssClass' property which is set through simple databinding
					return "button1024";
				}
				else if( Request.Form["ClientResolution"] == "800Res")
				{
					//Set the Html Hidden Input Control Value For Reffering on Client Through JavaScript
					ServerResolution.Value="800Res";
					//Returning CSS class name according to client's resolution for Button control for setting its 'CssClass' property which is set through simple databinding
					return "button800";
				}
				else
				{
					//if client resolution is not detected yet, then set default 1280x1024 resolution
					return "button1280";
				}
			}
		}

		//this property (Label for representing Label control :) ) is used for simple databinding of css value to the Label control property
		public string LB
		{
			get
			{
				//if page is first time requested, then set the default resolution setting as 1280x1024
				if (! Page.IsPostBack )
				{
					//Set the Html Hidden Input Control Value For Reffering on Client Through JavaScript
					ServerResolution.Value="1280Res";
					//Returning CSS class name according to client's resolution for Label control for setting its 'CssClass' property which is set through simple databinding
					return "label1280";					
				}
				else if( Request.Form["ClientResolution"] == "1280Res")
				{
					//Set the Html Hidden Input Control Value For Reffering on Client Through JavaScript
					ServerResolution.Value="1280Res";
					//Returning CSS class name according to client's resolution for Label control for setting its 'CssClass' property which is set through simple databinding
					return "label1280";
				}
				else if( Request.Form["ClientResolution"] == "1024Res")
				{
					//Set the Html Hidden Input Control Value For Reffering on Client Through JavaScript
					ServerResolution.Value="1024Res";
					//Returning CSS class name according to client's resolution for Label control for setting its 'CssClass' property which is set through simple databinding
					return "label1024";
				}
				else if( Request.Form["ClientResolution"] == "800Res")
				{
					//Set the Html Hidden Input Control Value For Reffering on Client Through JavaScript
					ServerResolution.Value="800Res";
					//Returning CSS class name according to client's resolution for Label control for setting its 'CssClass' property which is set through simple databinding
					return "label800";
				}
				else 
				{
					//if client resolution is not detected yet, then set default 1280x1024 resolution
					return "label1280";
				}
			}
		}	
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion	

		
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

About the Author

Faheem Iqbal
Web Developer
Pakistan Pakistan
Faheem is a .net web developer and freelancer working for a canadian development centre named Acumen Prescience. He enjoys playing video games, listening to linkin park, and having good sincere buddies. He has also worked on AJAX and hopes that Microsoft gets some common sense one day and provide developers with AJAX technology in its development platforms !

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 13 Apr 2006
Article Copyright 2006 by Faheem Iqbal
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid