The method used in this example can work after copy paste in the Visual Studio.
Aspx Code
<br /> <h5> <asp:Literal ID="Literal1" runat="server"></asp:Literal> </h5> <p> </p> <p> </p>
Code Behind
private string SAFARI_CODE = "Safari"; private string CHROME_CODE = "Chrome"; private string IE_CODE = "IE"; private string IE_LONGCODE = "Internet Explorer"; private string GECKO_CODE = "Gecko"; private string OPERA_CODE = "Opera"; protected void Page_Load(object sender, EventArgs e) { Literal1.Text = GetBrowserClass(true); } /// <summary> /// Gets the browser specific CSS class name. /// </summary> /// <param name="includeVersion">Indicates if the browser version should be included</param> public string GetBrowserClass(bool includeVersion) { string result = null; if (HttpContext.Current != null) { // Get browser HttpBrowserCapabilities oBrowser = HttpContext.Current.Request.Browser; string userAgent = HttpContext.Current.Request.UserAgent; if ((oBrowser != null) && (oBrowser.Browser != null) && (userAgent != null)) { if (!oBrowser.Crawler) { // Get major version of the browser int majorVersion = GetMajorVersion(); string version = null; if (includeVersion) { version = (majorVersion > 0) ? majorVersion.ToString() : ""; } // Internet Explorer if ((oBrowser.Browser == IE_CODE) || (oBrowser.Browser == IE_LONGCODE)) { result = IE_CODE + version; } // Chrome else if (oBrowser.Browser.Contains(CHROME_CODE) || userAgent.Contains(CHROME_CODE)) { result = String.Format("{0}{1} {2}{1}", SAFARI_CODE, version, CHROME_CODE); } // Safari else if (oBrowser.Browser.Contains(SAFARI_CODE)) { result = SAFARI_CODE + version; } // Opera else if (oBrowser.Browser.Contains(OPERA_CODE)) { result = OPERA_CODE + version; } // Firefox else if (userAgent.Contains(GECKO_CODE)) { result = GECKO_CODE + version; } } } } return result; } /// <summary> /// Gets browser major version. /// </summary> public int GetMajorVersion() { // Get major version safely int majorVersion = 0; try { majorVersion = HttpContext.Current.Request.Browser.MajorVersion; } catch { } return majorVersion; }
Output Example :
Chrome :
IE 11
Mozilla Firefox
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)
0 comments:
Post a Comment