How To Check Browser In JavaScript

Introduction

Today in my office while coding I came across a situation where I want to check the session variable in my aspx page. That was because I need to assign different style-sheets according to the languageId.
Lets say if the language is Russian I need to add the Russian style-sheet to the page and if it is English I need to add the English style sheet.

So I though to mention this tip here so that if someone can be beneficial by using this one.



Behind The Scene

First of all Lets go to visual studio and create a new project ( web application ). Now add a new page to it and don't inherit it from any master-page ( if you inherit it from any master-page then all the ID of the page will be modified according to the content placeholder id, you can also fix this by providing clientIDMode = "static" ).

Now add the bellow code inside the form tag of the body section of the page.

<div>
        <h1 style="color: Red;">
            Wel come to tapan kumar's blog..........</h1>
        <h3>
            This example helps you understand how to access session variable in aspx page.</h3>
        <asp:Label runat="server" ID="lblSession"></asp:Label><br />
        <div id="pLanguage" style="border:2px solid blue;width:500px;">
            <h3>
                This paragraph's background color is <b style="color: Red">Gray</b> when the language
                English.</h3>
            <h3>
                Background color will be <b style="color: Red">white</b> when language is French.</h3>
            Wel come to tapan kumar's blog...........<br /><br />
        </div>
        <br /><br />
    </div>
    <table>
        <tr>
            <td>
                <asp:Button ID="Button1" runat="server" Text="Click Here To Change Session To English"
                    OnClick="btn_onclick" />
            </td>
            <td>
                <asp:Button ID="Button2" runat="server" Text="Click Here To Change Session To French"
                    OnClick="btn2_onclick" />
            </td>
        </tr>
    </table>


As you can see here I have added div which tells you that its background color will be changed according to the language set in the session from code behind.

Now we are taking two buttons for visualising the effects how we are accessing the session in aspx page. One button changes the session to English and another one to French.

When the language set in in the session is English then the div has a background color of  Gray  and when French then a background color of  White.

Now add the below code to the header part of the aspx page.


<title>Accessing session variable in aspx page</title>
    <% if (Session["Language"] != null && Session["Language"].ToString().Equals("English"))
        {   
    %>
    <style type="text/css">
        #pLanguage
        {
            background-color: Gray;
        }
    </style>
    <% 
        }
    %>

Here we are checking the server-side session variable and assigning a style to the div with ID pLanguage

Now in code behind paste the below methods;

protected void Page_Load(object sender, EventArgs e)
        {
            //Set the session as French
            Session["Language"] = "French";
            lblSession.Text = "Current Session is : French";
        }

        protected void btn_onclick(object sender, EventArgs e)
        {
            //Set the session as English
            Session["Language"] = "English";
            lblSession.Text = "Current Session is : English";
        }

        protected void btn2_onclick(object sender, EventArgs e)
        {
            //Set the session as French
            Session["Language"] = "French";
            lblSession.Text = "Current Session is : French";
        }

As you can see in the code behind I am setting the session variable named Language to French in page load and then changing this to English and French according to the button clicked on the page.

After all this you can see the result by running your code and you can find that you have accessed the server side session variable in your aspx page. Or also you can download the source-code attached above and run it to see the example.

Happy Coding...

No comments:

Post a Comment