Thursday, October 28, 2010

Change CSS in Sharepoint Master Page File depends on Current Browser and Current Platform

One CSS will never work for every platform and Browser for same look and feel of the application, we need different CSS depends on platform and Browser, because every browser has their own behavior.
So for Consistent Look and Feel Across the platform, we have to assign the appropriate CSS depends on detecting platform and browser , and we have to tell master page which css they have to refer so it gives nice look.
To solve this problem, we have written one webpart named as "Browser Finding” and that webpart has been added in master page of the application.
By these lines, it will identify the current browser and platform
string currentUserBrowser = this.Context.Request.Browser.Browser;
string currentUserPlatform = this.Context.Request.Browser.Platform;
Let us see some code of portion of the code.
HtmlGenericControl linkNewCore = new HtmlGenericControl("link");
HtmlGenericControl linkMyStyleSheet = new HtmlGenericControl("link");
linkNewCore.Attributes.Add("rel", "stylesheet");
linkMyStyleSheet.Attributes.Add("rel", "stylesheet");
linkNewCore.Attributes.Add("type", "text/css");
linkMyStyleSheet.Attributes.Add("type", "text/css");
linkNewCore.Attributes.Add("href", "/Style Library/newcore.css");
linkMyStyleSheet.Attributes.Add("href", "/Style Library/gso-search.css");

string sUniqueQuery = "?rev=" + Guid.NewGuid().ToString().Replace("-", "");
string currentUserBrowser = this.Context.Request.Browser.Browser;

            switch (currentUserBrowser)
                case "AppleMAC-Safari":
                    linkMyStyleSheet.Attributes.Add("href", "/Style Library/Safari.css" + sUniqueQuery);
                case "Firefox":
                    switch (currentPlatform)
                        case "Unknown":
                            linkMyStyleSheet.Attributes.Add("href", "/Style Library/Netscape_Mac.css" + sUniqueQuery);
                            linkMyStyleSheet.Attributes.Add("href", "/Style Library/Netscape.css" + sUniqueQuery);
                    linkMyStyleSheet.Attributes.Add("href", "/Style Library/Custom.css" + sUniqueQuery);
Disha Shah

No comments:

Post a Comment