3

General idea

My general idea is to have mobile and desktop version of my site. Users can switch versions with buttons on the bottom of the page. I'm using ASP themes, so that I can easily switch themes depending on the required version of the site.

The problem

Switching themes is great, but since I've got JavaScript files already included in my project in the following ScriptManager in my master page:

<asp:ScriptManager runat="server" ID="sm">
        <Scripts>
            <asp:ScriptReference Path="~/Scripts/jquery-2.0.2.min.js" />
            <asp:ScriptReference Path="~/Scripts/jQueryMobileBehaviour.js" />
            <asp:ScriptReference Path="~/Scripts/Master.js" />
            <asp:ScriptReference Path="~/Scripts/jquery.mobile-1.3.1.min.js" />
        </Scripts>
</asp:ScriptManager>

When the user switch to desktop version there are problems caused by jquery.mobile-1.3.1.min.js and jQueryMobileBehaviour.js. Is there a way to use two ScriptManagers (some sort of themes but for js files)?

What I've tried without success

My first approach was to remove the mobile JavaScript files from the ScriptManager and then include them manually in the click event of the button that switches to mobile version like that sm.Scripts.Add.

The second approach was to remove programmatically the mobile JavaScript files like that sm.Scripts.Remove.

    protected void CommandBtn_Click(Object sender, CommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "Desktop":
                HttpContext.Current.Response.Cookies["theme"].Value = "Desktop";
                //sm.Scripts.Remove(new ScriptReference("~/Scripts/jquery.mobile-1.3.1.min.js"));
                break;
            case "Mobile":
                HttpContext.Current.Response.Cookies["theme"].Value = "Mobile";                    
                //sm.Scripts.Add(new ScriptReference("~/Scripts/jquery-2.0.2.min.js"));
                //Response.Redirect(Request.RawUrl);
                break;
            default:
                break;

        }
        Page.Response.Redirect(Page.Request.Url.ToString(), true);
    }

Both approaches didn't work.

To sum up the questions

  • Is there something wrong in my code - assuming that the paths should be alright?
  • Is there a better approach, which will allow me to switch JavaScript files, like is done with the themes?
4
  • Maybe if you add two <asp:ScriptManager runat="server" ID="sm"> with two different setup and two different ids, and you swap them (one visible, one hidden) base on your them. Commented Aug 20, 2013 at 10:43
  • @Aristos I cannot add two ScriptManagers on one page. I'm currently looking for a workaround, but if you know how to add multiple ScriptManagers, please tell me. Commented Aug 20, 2013 at 11:17
  • 1
    @Aristos Thanks for your help I managed to solve my problem. I've added my solution below. Commented Aug 20, 2013 at 12:33
  • 1
    I am glad that I help you that way... Commented Aug 20, 2013 at 14:55

1 Answer 1

2

I finally came up with a solution. I've tried adding two <asp:ScriptManager runat="server" ID="sm"> and make them sm.Visible = true/false depending on the version of the site as @Aristos suggested. However I could not use two ScriptManagers on the same page and also there is no property Visible of the ScriptManager.

So here is what I've done.

Firstly since I'll need to switch between two sets of scripts I've made two separate ScriptManagerProxy (because I could not have two ScriptManagers).

For the Desktop version:

    <asp:ScriptManagerProxy runat="server" ID="smDesktop">
        <Scripts>
            <asp:ScriptReference Path="~/Scripts/jquery-2.0.2.min.js" />
            <asp:ScriptReference Path="~/Scripts/Modernizr.js" />
            <asp:ScriptReference Path="~/Scripts/Modernizr_full.js" />
            <asp:ScriptReference Path="~/Scripts/Master.js" />
        </Scripts>    
    </asp:ScriptManagerProxy>

and for the Mobile version:

<asp:ScriptManagerProxy runat="server" ID="smMobile">
    <Scripts>
        <asp:ScriptReference Path="~/Scripts/jquery-2.0.2.min.js" />
        <asp:ScriptReference Path="~/Scripts/jQueryMobileBehaviour.js" />
        <asp:ScriptReference Path="~/Scripts/Modernizr.js" />
        <asp:ScriptReference Path="~/Scripts/Modernizr_full.js" />
        <asp:ScriptReference Path="~/Scripts/Master.js" />
        <asp:ScriptReference Path="~/Scripts/jquery.mobile-1.3.1.min.js" />
    </Scripts>
</asp:ScriptManagerProxy>

IMPORTANT PART STARTS HERE

Then I put them in two separate user controls, which I registered to the master page:

<%@ Register Src="~/UserControl/ScriptManagerDesktop.ascx" TagName="smDesktop" TagPrefix="uc" %>
<%@ Register Src="~/UserControl/ScriptManagerMobile.ascx"TagName="smMobile" TagPrefix="uc" %>

Then in the master page body I've inserted a ContentPlaceHolder, which I'll use to insert one of the user controls depending, which version is required.

    <asp:ScriptManager runat="server" ID="sm"></asp:ScriptManager>
    <asp:ContentPlaceHolder ID="cphScripts" runat="server">
    </asp:ContentPlaceHolder>

and finally in the master's page codebehind I'm adding the needed user control to the placeholder:

                if (HttpContext.Current.Request.Cookies["theme"] != null)
                {
                    switch (HttpContext.Current.Request.Cookies["theme"].Value)
                    {
                        case "Desktop":
                            cphScripts.Controls.Add(Page.LoadControl("~/UserControl/ScriptManagerDesktop.ascx"));
                            break;
                        case "Mobile":
                            cphScripts.Controls.Add(Page.LoadControl("~/UserControl/ScriptManagerMobile.ascx"));
                            break;
                        default:
                            cphScripts.Controls.Add(Page.LoadControl("~/UserControl/ScriptManagerDesktop.ascx"));
                            break;
                    }
                }

And voila I've got your ScriptManager switcher ready to go.

Hope this will help someone.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.