1

This markup supports code that is not rendered in the mobile version of the page on which I'm working.

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<%@ Register TagPrefix="ComponentArt" Namespace="ComponentArt.Web.UI" Assembly="ComponentArt.Web.UI" %>
<%@ Register Src="Cart.ascx" TagName="Cart" TagPrefix="uc1" %>
<%@ Register Src="Search.ascx" TagName="Search" TagPrefix="uc5" %>
<%@ Register Src="Credits.ascx" TagName="Credits" TagPrefix="uc6" %>
<%@ Register Src="Menu.ascx" TagName="Menu" TagPrefix="uc3" %>
<%@ Register Src="taskspanel.ascx" TagName="TasksPanel" TagPrefix="uc6" %>

Unsurprisingly a lot of unnecessary scripts are loaded. This is bad.

If there is markup to make these registrations conditional please refer me to documentation. Otherwise, my current thought is to use explicit code equivalent to the markup, and make it conditional that way.

I've discovered ScriptManager from this rather old resource but the above markup doesn't reference js files explicitly so I don't know how to map between the two. Can anyone advise?

Attempting to use superjos' response below I modified my page to reference Page.Mobile, a Boolean property conveniently already in place:

<%@ Page Language="C#" AutoEventWireup="false" EnableViewStateMac="false" CodeBehind="Forms.aspx.cs" Inherits="ASSAOHS.forms" %>
<%@ if (!Mobile) { %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<%@ Register TagPrefix="ComponentArt" Namespace="ComponentArt.Web.UI" Assembly="ComponentArt.Web.UI" %>
<%@ Register Src="Cart.ascx" TagName="Cart" TagPrefix="uc1" %>
<%@ Register Src="Search.ascx" TagName="Search" TagPrefix="uc5" %>
<%@ Register Src="Credits.ascx" TagName="Credits" TagPrefix="uc6" %>
<%@ Register Src="Menu.ascx" TagName="Menu" TagPrefix="uc3" %>
<%@ Register Src="taskspanel.ascx" TagName="TasksPanel" TagPrefix="uc6" %>
<%@ } %>

but this produces Parser Error Message: The server block is not well formed.

Writing it like this

<%@ Page Language="C#" AutoEventWireup="false" EnableViewStateMac="false" CodeBehind="Forms.aspx.cs" Inherits="ASSAOHS.forms" %>
@if (!Mobile) {
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<%@ Register TagPrefix="ComponentArt" Namespace="ComponentArt.Web.UI" Assembly="ComponentArt.Web.UI" %>
<%@ Register Src="Cart.ascx" TagName="Cart" TagPrefix="uc1" %>
...
}

doesn't produce an error but isn't processed; the if statement and braces are simply passed through and rendered as text at the browser.

Which leaves me back with option two, map the directives to explicit code using ScriptManager to register stuff. Any takers?


Thanks to Pedro for trying to help but that reference doesn't help with how to express those control directives as code.

It looks like I may need to use option three, which redirect to another page that doesn't contain the directives.

Peter Wone
  • 17,965
  • 12
  • 82
  • 134

1 Answers1

0

Have you tried with this SO answer? Basically by having some value conditionally taking a true/false value in C# code:

public bool IsDebugBuild 
{ 
    get
    {
        #if DEBUG
        return true;
        #else
        return false;
        #endif
    }
}

and then using only that bit from code as an @if condition in Razor, keeping all the rest still in Razor?

Another answer is saying the same as well.

Disclaimer: I haven't tried this, and I've not been playing with <%@ Register> directives for a looooong time.

superjos
  • 12,189
  • 6
  • 89
  • 134
  • I already have a flag of the form you suggest and I'm about to try your answer. Thanks for compensating my ignorance of Razor syntax. – Peter Wone Jul 27 '17 at 00:15
  • I'm just not sure it will work with `Register` directives. If it's a matter of Razor syntax, try looking around for *Razor if then else*, e.g. [here](http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx/) – superjos Jul 27 '17 at 00:18