How to express directives as C# so they can be conditional - c#

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.

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.

Related

An entry with the same key already exists, but no duplicates in resources present

After returning from the holidays, I get the same error message as in the title:
An entry with the same key already exists.
The error points to this line:
<asp:Label ID="lblPersDesc" Text="Placeholder irrelevant" runat="server" meta:resourceKey="lblPersDesc"/>
If I comment this line out, the error appears on the next element with a meta:resourceKey property.
Taken steps to rectify:
Check markup for duplicate Ids
Check database table for duplicate resource Keys
Clear & Rebuild solution
force recompile of the .aspx page
clear resourceProvider cache
Where could the error be located and what could I have been missing?
EDIT: More page markup.
<%# Page Language="C#" MasterPageFile="~/Master.master" AutoEventWireup="true" CodeFile="~/.../...cs" Inherits="..." Title="..." %>
<%# MasterType VirtualPath="~/Master.master" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>
<%# Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<asp:Content ID="Content2" ContentPlaceHolderID="cont" Runat="Server">
</asp:Content>

ASP.Net Button Click Event Handler not auto generated in code-behind, placed in script tags on page instead

I've been wrestling with this all day. Opening up an old web application trying to add a method to my page (default.aspx) and have my code behind default.aspx.cs which is referenced in the page:
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="HoursWorked._Default" %>
<%# Import Namespace="HoursWorked" %>
Some of the symptoms I'm having are intellisense not "seeing" any of the controls from the page, when I double click the button control in the Designer, it creates the following:
<script runat="server">
protected void ButtonShowUnderEight_Click(object sender, EventArgs e)
{
}
</script>
Code behind starts off as (after all of my using statements):
namespace HoursWorked
{
public partial class _Default : Page
{
Any ideas?
Thanks,
Tim
In my case (VS2013) what helped was based on this link:
Remove CodeBehind="yourfile.aspx.cs" from the first line of your
.aspx file which will cause the c# event handler code to be placed into
your .aspx source file instead.
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="HoursWorked._Default" %>
So you are left with:
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" Inherits="HoursWorked._Default" %>
Now double-click the control in the Designer which should now pop the event handler code inside script tags within your aspx.
Next, restore the CodeBehind="yourfile.aspx.cs" you had removed in the first step.
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="HoursWorked._Default" %>
Finally, double-click the control which now pops the event handler code inside yourfile.aspx.cs!
Replace
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="HoursWorked._Default" %>
with
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="._Default" %>
It does not like when you inherit
Inherits="HoursWorked.Default"
P.S, you can also remove
<%# Import Namespace="HoursWorked" %>
I ended up going back to my original local solution instead of trying to use the ones on my dev and prod servers. No clue why they were having this issue compared to my original local solution.
Not going to help anyone else having this issue, but instead of investing more time trying to solve this... I'm sure if I compared everything in my solution folder, something might stick out.
Tim

Added scripts do not appear in HTML when visiting site

DNN Version: 04.09.05 (yes, I know it's old. This is one of the oldest sites maintained by the company I work for)
I'm not entirely sure what code to include here, since it seems almost irrelevant. Here's my issue:
I have added a mobile redirect script to a the skin.ascx for a DNN site:
<%# Control language="vb" CodeBehind="~/admin/Skins/skin.vb" AutoEventWireup="false" Explicit="True" Inherits="DotNetNuke.UI.Skins.Skin" %>
<%# Register TagPrefix="dnn" TagName="LOGO" Src="~/Admin/Skins/Logo.ascx" %> <%# Register TagPrefix="dnn" TagName="SOLPARTMENU" Src="~/Admin/Skins/SolPartMenu.ascx" %>
<%# Register TagPrefix="dnn" TagName="USER" Src="~/Admin/Skins/User.ascx" %> <%# Register TagPrefix="dnn" TagName="LOGIN" Src="~/Admin/Skins/Login.ascx" %>
<%# Register TagPrefix="dnn" TagName="CURRENTDATE" Src="~/Admin/Skins/CurrentDate.ascx" %>
<%# Register TagPrefix="dnn" TagName="LANGUAGE" Src="~/Admin/Skins/Language.ascx" %>
<%# Register TagPrefix="dnn" TagName="SEARCH" Src="~/Admin/Skins/Search.ascx" %>
<%# Register TagPrefix="dnn" TagName="BREADCRUMB" Src="~/Admin/Skins/BreadCrumb.ascx" %>
<%# Register TagPrefix="dnn" TagName="LINKS" Src="~/Admin/Skins/Links.ascx" %> <%# Register TagPrefix="dnn" TagName="COPYRIGHT" Src="~/Admin/Skins/Copyright.ascx" %>
<%# Register TagPrefix="dnn" TagName="TERMS" Src="~/Admin/Skins/Terms.ascx" %> <%# Register TagPrefix="dnn" TagName="PRIVACY" Src="~/Admin/Skins/Privacy.ascx" %>
<%# Register TagPrefix="dnn" TagName="HELP" Src="~/Admin/Skins/Help.ascx" %>
<%# Register TagPrefix="dnn" TagName="HOUSEMENU" Src="~/DesktopModules/HouseMenuSkinObject/HouseMenuSkinObject.ascx" %>
<!-- Mobile Redirect -->
<script src="/Portals/_default/skins/*********/scripts/redirection-mobile.js"></script>
<script>
SA.redirection_mobile();
</script>
<!-- / Mobile Redirect -->
<!-- www.DotNetNukeSkin.com, www.DNNSource.com, www.DNNBlast.com -->
but despite my efforts to clear the DNN cache and even restart the application, this never appears when visiting the site.
Here's what I see where that script should be:
<script src="/WebResource.axd?d=Vfg8rdbqjJxftEOai7TpkVMgS5M3EpWnLJOIYxski_LUgp8dX_IpQo23OtpIMPbl9BRn7Gvd5xGcDwPU0&t=635359717876031270" type="text/javascript"></script>
<script src="/js/dnncore.js" type="text/javascript"></script>
<script src="/ScriptResource.axd?d=EHk-FrMHceUyqcY0-Ou7uoHZdqiXevkiBXGM4IwtB9QaoQazyRuYSotYRd4XIARQzPydSQ5xOERE5RYgV-OvRozQDW8PaDmPnWhQI1UYydn9DyMcGUSN9-XtqJ5WOQA1G57ez5pQ9bzPvHpo6Wyh8yUGgFY1&t=ffffffffa37d832a" type="text/javascript"></script>
<script src="/ScriptResource.axd?d=aMtM2AA2yI3ghP5Pe-NcoK-AYskpYVGguYXvrwDo1MUnUBn18bqEEGw8_GdcQ79Ecs7d5GcFroKs5--e-ZmUr3jRRMfTGboqIz4N8yPYJIojGGy6AnIDkQR4Hp1W3z4jIWzDpSXifOa6inrF-rghB-sjnyjG9wsURK0oHw2&t=ffffffffa37d832a" type="text/javascript"></script>
<!-- www.DotNetNukeSkin.com, www.DNNSource.com, www.DNNBlast.com -->
Any ideas?
You would typically need to verify that ANYTHING you put into the HTML shows up. I imagine that you will find that it won't.
If that's the case, you'll need to track down which Skin is actually in use, and make sure you're changing the right one.
The skin could be in the path you mention above
/portals/_default/skins/skinname/
but it could also be pulling from
/portals/#/skins/skinname where # is the ID of the Portal.
So basically, start by verifying you're in the right place, then make changes from there. The HTML you're putting in shouldn't cause any problems with DNN itself, so that leads me to believe you're simply not looking in the right place..

user control not rendering content of ascx

i thought this was a simple issue until i started searching for answers and realised it's so simple i'm the only one who has it
my user control isnt displaying anything. what am i doing wrong? (besides letting this be my life...)
control:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="ctrl.ascx.cs" Inherits="proj.UserControls.ctrl" %>
asdjkldasfjasdfljdfasjklasdfjkl
use:
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="page.aspx.cs" Inherits="proj.Admin.page" %>
<%# Register assembly="proj" namespace="proj.UserControls" tagprefix="cc1" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<cc1:ctrl ID="test" runat="server" />
</asp:Content>
Change:-
<%# Register assembly="proj" namespace="proj.UserControls" tagprefix="cc1" %>
To
<%# Register TagPrefix="cc1" TagName="ctrl" Src="/path/to/ctrl.ascx" %>
You're missing TagName, which represents the text following the colon in the control declaration. You're also not telling the engine where to find the source file ( Src attribute ). Change /path/to to represent the path from root to your control.
IF you have created custom control then you should add reference of the dll of your custom control ( from choose items from ToolBox of Visual Studio). and then Use the following tag in the page :
<%# Register assembly="proj" namespace="proj.UserControls" tagprefix="cc1" %>
If you created User Control then add the following line in your page:
<%# Register src="~/UserControls/ctrl.ascx" TagName="ctrl" tagprefix="cc1" %>
instead of
<%# Register assembly="proj" namespace="proj.UserControls" tagprefix="cc1" %>
use
<%# Register src="~/UserControls/ctrl.ascx" TagName="ctrl" tagprefix="cc1" %>
Make sure you haven't set visible="false" on a panel or div containing your control.
That would've saved me a good hour.
I had to add a new user control to an existing project that already contained a lot of them, and wondered why mine was not rendering. Turns out you can also specify this in Web.config, under configuration, system.web, pages, like this:
<controls>
<add tagPrefix="cc1" tagName="ctrl" src="~/UserControls/ctrl.ascx" />
...which will register the control for the whole project, so you don't have to specify this on every page. Useful for controls that are heavily reused.

Moving code around results in "unrecognized tag prefix or design filter" error

I have a Visual Web Developer 2010 Express Edition Web Application. The Default.aspx starts off as follows:-
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MixedZone.Default"%>
<%# Register assembly="ComponentArt.Web.UI"
namespace="ComponentArt.Web.UI"
tagprefix="ComponentArt" %>
<%# Register TagPrefix="MixedZone"
TagName="NumberInput"
Src="Common/UserControls/NumberInput.ascx" %>
The NumberInput.ascx starts like this:-
<%# Control Language="C#" AutoEventWireup="True" CodeBehind="NumberInput.ascx.cs" Inherits="MixedZone.UserControls.NumberInput" %>
<%# Register assembly="ComponentArt.Web.UI" namespace="ComponentArt.Web.UI" tagprefix="ComponentArt" %>
<asp:Literal ID="Literal1" runat="server" Visible="False"></asp:Literal>
<ComponentArt:NumberInput ID="niNumberInput"
runat="server"
CssClass="valid"
...etc
So far, so good. But I wish to move this code to a different directory. I do this (editing the csproj file by hand) and change the aspx and aspc files accordingly:-
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MixedZone.Default"%>
<%# Register assembly="ComponentArt.Web.UI"
namespace="ComponentArt.Web.UI"
tagprefix="ComponentArt" %>
<%# Register TagPrefix="MixedZone"
TagName="NumberInput"
Src="../MixedZone/Common/UserControls/NumberInput.ascx" %>
and
<%# Control Language="C#" AutoEventWireup="True" CodeBehind="../MixedZone/Common/UserControls/NumberInput.ascx.cs" Inherits="MixedZone.UserControls.NumberInput" %>
<%# Register assembly="ComponentArt.Web.UI" namespace="ComponentArt.Web.UI" tagprefix="ComponentArt" %>
<asp:Literal ID="Literal1" runat="server" Visible="False"></asp:Literal>
<ComponentArt:NumberInput ID="niNumberInput"
runat="server"
CssClass="valid"
...etc
but when I do so, the ComponentArt: is marked with the green underline, and the message
Unrecognized tag prefix or device filter 'ComponentArt'
(about five hundred compilation errors appear as a result). The directory Common\UserControls was moved in its entirety, and nothing else has been changed. Does anyone have any idea what the problem might be and what I might do about it?
I worked around this problem by using SubVersion to include the code in the old location even though it isn't "really" there. I don't like this very much as it seems like a clever-clever trick and a trap for future maintenance. But it won't have the directory if it isn't below the project's, and it won't follow a shortcut if I place one there.

Categories

Resources