jump to navigation

How to change the master page depending on the client’s browser October 6, 2010

Posted by fofo in Visual Studio 2005, Visual Studio 2008, Visual Studio 2010.
Tags: ,
1 comment so far

In one of my seminars on ASP.Net, I was asked a question from a student regarding master pages and the various browser appications.

He basically asked if it was possible to have a different master page depending on the client’s browser.

My initial answer was to find the browser’s version programmatically and according to this value to have a particular master page dynamically assigned during the PreInit stage of the Page lifecycle.

But there is a simpler way.At first we need to create a website using Visual Studio 2008/2010.

Then copy the original Site.master page to the root directory of the website and rename it to AnotherSite.master.

Then we need to copy and paste in the root direcotory the Site.css and rename it to NewSite.css.

So we have now two master pages and two .css files.

Edit the AnotherSite.master and change the css reference

from

 <link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
to
 <link href="~/Styles/NewSite.css" rel="stylesheet" type="text/css" />

Now we do need to make some changes to the Newsite.css file.  You can change the background color, for example

body   
{
background:#efefef;

The page layout

.page
{
    width: 1200px;

Now we can go add a new web form to our page. We will call it “Anotherpage.aspx”.

Select as a master page the AnotherSite.master.

Then go to the “Source” of the newly created page and edit the Page directive according to the following line.

<%@ Page Title="" Language="C#"
mozilla:MasterPageFile="~/AnotherSite.master" 
ie:MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="MyNewPage.aspx.cs" Inherits="MyNewPage" %>

Run your application with IE and Firefox and notice the respective masterpages being applied to the web page.

Hope it helps!!!

Caching an asp.net page depending on the user’s browser language October 6, 2010

Posted by fofo in asp.net, VB 2005, VB 2008, Visual Studio 2008, Visual Studio 2010.
Tags: ,
add a comment

This is my second post regarding caching in asp.net applications.

A friend of mine asked me if he can control caching of an asp.net page depending on the user’s language browser.

Let’s see how we can accomplish that with a step by step example.

You can use any of the versions of Visual studio (VS 2005,VS2008,VS2010 express editions work fine).

I will use VS 2010 Ultimate edition and VB.net as the .Net language of choice.

We will identify the language of the user’s browser from the HTTP headers.

More specifically we will look at the Accept-Language header item.

1) Create an Asp.net web site and name it as you want.Add a label web server control to your default.aspx page.

2) In the default.aspx just below the Page directive ( top of the page ) you add the following bit of code:

<%@ OutputCache Duration="50" VaryByHeader="Accept-Language"
VaryByParam="none"
 %>

We want to use VaryByHeader and assign it to the header item Accept-Language that we want to vary caching by.

So now this page is going to be cached based on the language of the user’s browser.

3) Now switch back to the code behind and in the Page_Load event handling routine type the following

If Not (Request.Headers("Accept-Language")) Is Nothing Then 
mylabel.Text = "language of the user is " +
 Request.Headers("Accept-Language").ToString
End If

4) Run your application and see the result-your browser’s language. In most cases it will be something like “en-us“, or anything else that the HTML page will output. So if now we have 20 people

hitting our page – within the 50 seconds cache period – and all of them have “en-us” as their browser’s language they are going to get the cached version of the page. The page will not be re-rendered through the whole

page events-lifecycle.

5) If we have people hitting our page with a browser with a different language (.eg fr,es ) the Asp.Net would cache a second version of the page in french. So now we would have 2 cached versions of the page, one in english and one in french.So the first french person (browser language) to hit the page, he would see the page rendered to him ( the whole page lifecycle will run ) and at the same time it will be cached so subsequent requests from french people-users-browsers will be served from the cache.

If you need the code, just email me.

Hope it helps !!!

Change the master page depending on the client’s browser August 24, 2010

Posted by fofo in asp.net.
Tags: ,
add a comment

In one of my seminars on ASP.Net, I was asked a question from a student regarding master pages and the various browser appications.

He basically asked if it was possible to have a different master page depending on the client’s browser.

My initial answer was to find the browser’s version programmatically and according to this value to have a particular master page dynamically assigned during the PreInit stage of the Page lifecycle.

But there is a simpler way.At first we need to create a website using Visual Studio 2008/2010.

Then copy the original Site.master page to the root directory of the website and rename it to AnotherSite.master.

Then we need to copy and paste in the root direcotory the Site.css and rename it to NewSite.css.

So we have now two master pages and two .css files.

Edit the AnotherSite.master and change the css reference

from

<link href=”~/Styles/Site.css” rel=”stylesheet” type=”text/css” />

to

<link href=”~/Styles/NewSite.css” rel=”stylesheet” type=”text/css” />
Now we do need to make some changes to the Newsite.css file.
you can change the background color, for example

body
{
background:#efefef;
….

The page layout

.page
{
width: 1200px;

Now we can go add a new web form to our page. We will call it “Anotherpage.aspx”.

Select as a master page the AnotherSite.master.

Then go to the “Source” of the newly created page and edit the Page directive according to the following line.

<%@ Page Title=”” Language=”C#” mozilla:MasterPageFile=”~/AnotherSite.master”
ie:MasterPageFile=”~/Site.master” AutoEventWireup=”true”
CodeFile=”MyNewPage.aspx.cs” Inherits=”MyNewPage” %>

Run your application with IE and Firefox and notice the respective masterpages
being applied to the web page.
Hope it helps!!!

Retrieve browser information from an asp.net(vb) web application June 24, 2008

Posted by fofo in asp.net, asp.net 2.0, VB 2005, Visual Studio 2008.
Tags:
2 comments

In a recent project of mine, I had to collect information on what are the browser capabilities of a given user who would access through that browser the web application.

You can accomplish what i am going to show you with javascript. But i approached it differently and i will show you how to do that with a asp.net web application.

So we must launch visual studio and then create a asp.net web application. I am going to use VB.

When we create a new web application,we have the Default.aspx page.

we go to our code-behind page, so we are in the the Default.aspx.vb page and in the Page Load event we can write the following code

Dim myusersbrowser As HttpBrowserCapabilities = Request.Browser

        Response.Write(“general browser family” + ” : ” + myusersbrowser.Browser)
        Response.Write(“</br>”)
        Response.Write(“browser type” + ” : ” + myusersbrowser.Type)
        Response.Write(“</br>”)
        Response.Write(“supports javascript” + ” : ” + myusersbrowser.JavaScript.ToString())
        Response.Write(“</br>”)
        Response.Write(“javascript version” + ” : ” + myusersbrowser.JScriptVersion.ToString())
        Response.Write(“</br>”)
        Response.Write(“does the browser support cookies?” + ” : ” + myusersbrowser.Cookies.ToString())
        Response.Write(“</br>”)
        Response.Write(“search engine web crawler?” + ” : ” + myusersbrowser.Crawler.ToString())
        Response.Write(“</br>”)
        Response.Write(“is this a mobile browser?” + ” : ” + myusersbrowser.IsMobileDevice.ToString())
        Response.Write(“</br>”)
        Response.Write(“Active X controls enabled?” + ” : ” + myusersbrowser.ActiveXControls.ToString())
        Response.Write(“</br>”)
        Response.Write(“Platform” + ” : ” + myusersbrowser.Platform)

I will try to explain what happens here. It is really simple.

We first have to create an instance(object) of the HttpBrowserCapabilities class. This class lives in the System.Web namespace.

In order to get an instance of this class we need to get hold of the Browser property of the Request object.

Dim myusersbrowser As HttpBrowserCapabilities = Request.Browser

then i use Response object and the Write method to write the information to the page using the various properties of the browser object I just created.

Response.Write(“browser type” + ” : ” + myusersbrowser.Type)

The line above gives us the Browser type, in my case IE7.

all the rest are straightforward. run the web application and you will see all the information. Here is how it looks when i run my application.

general browser family : IE
browser type : IE7
supports javascript : True
javascript version : 5.6
does the browser support cookies? : True
search engine web crawler? : False
is this a mobile browser? : False
Active X controls enabled? : True
Platform : WinNT

Please note that you can experiment with dozens of other properties of the  browser object.

This information can become very handy if ,for example, we have an Ajax enabled website but we still care for those people who have Javascript disabled. We can use portion of the code above to redirect our users accordingly. Those who have Javascript disabled they are redirected to our simple non-Ajax website.

Add to FacebookAdd to NewsvineAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to Ma.gnoliaAdd to TechnoratiAdd to Furl

Follow

Get every new post delivered to your Inbox.

Join 1,793 other followers