views:

45

answers:

4

I have two user controls in my ASP.Net project, one that is explictly for use with Internet Explorer 6 (IE6) and another that should be used when the user's browser is not IE6.

How would I go about setting it up so that this happens; is this something I can put in a master page?

+1  A: 

To get the browser you could use:

Response.Write(Request.Browser.Browser.ToString());
Response.Write(Request.Browser.Version.ToString());
hminaya
+2  A: 

Sniff the request headers in your content.master's code-behind page to figure out which browser, and set the Visible property on the control that you don't want to use to false. That's assuming that these two controls are hosted by the master page itself. If they only appear on some pages, then you can do the same thing, but in those individual pages rather than the master page.

Andrew Arnott
From the way the question was posed, seems like those usercontrols are contained within the masterpages.
o.k.w
+2  A: 

You can achieve this by using the Browser property of the HttpRequest object.

Your page can either have both user-controls sited at design time, then at run time, you inspect the Request.Browser property to determine the client's browser and programmatically hide the user-control you don't want the user to see.
Alternatively, you can instantiate and render the correct user-control (again, after inspecting the Request.Browser property) purely from your server-side code.

For example, running the following code:

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(Request.Browser.Type.ToString());

    }
}

In a "standard" ASPX page displays:

IE7

when run in Internet Explorer 7, and:

Firefox3.5.3

(when run in Firefox)

So, you could have code something like the following in the web-page that you want to add this functionality to:

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Browser.Type.ToString().ToLower().Contains("ie"))
        {
            // User's browser is Internet Explorer.
            // Let's hide UserControl1 but display UserControl1
            WebUserControl1.Visible = false;
            WebUserControl2.Visible = true;
        }
        else
        {
            // User's browser is something other than Internet Explorer.
            // Let's hide UserControl2 but display UserControl1.
            WebUserControl1.Visible = true;
            WebUserControl2.Visible = false;
        }
    }
}
CraigTP
Awesome solution, thankyou!
Goober
A: 

You could use the javascript navigator object. Wrap them into a div and then hide them or show them using jQuery.

Raúl Roa
Depending on the nature of the controls, might be too 'heavy' to render both all the time and letting the UI to hide one of them. Still a viable solution though.
o.k.w