Register User Control Issue - asp.net

I have a user control registered at the top of my page:
<%# Register Src="/Controls/User/Navbar.ascx" TagName="Navbar" TagPrefix="pmc" %>
and I reference it in my page like this:
<pmc:Navbar runat="server" id="navbar"></pmc:Navbar>
but it does not know what <pmc:Navbar is. I cannot figure out why.
I'm using VS 2008, in a Web Application Project.

Maybe you should specify the path with ~: ... Src="~/Controls/User/Navbar.ascx" ...

Remove either the initial slash from the path to the control, or better still, prefix it with "~" :
<%# Register Src="Controls/User/Navbar.ascx" TagName="Navbar" TagPrefix="pmc" %>
or
<%# Register Src="~/Controls/User/Navbar.ascx" TagName="Navbar" TagPrefix="pmc" %>
The first solution is flakey as it relies on the page existing in the root folder and the control existing below it. The second is the preferred as it will work from any page in your project.
You should also consider registering your user controls in your web.config, as it keeps things much neater, and tends to avoid path issues a little better.

Related

namespace of DNN's Language class

I am trying to configure internationalization for my DNN project. I have added two languages, and now I want to access DNN's Language control in order to make it visible on project's pages.
I have found a snippet
<dnn:LANGUAGE runat="server" ID="dnnLANGUAGE" ShowLinks="True" ...
and I tried to use it. Compiler does not recognize dnn: tagPrefix so I have to register it, that is, I have to pass class's namespace inside Register tag in my ascx page. But I can not find the namespace anywhere. I have also browsed www.dnnsoftware.com/dnn-api , but could not find it.
You need to add the following line at the top of your ascx page.
<%# Register TagPrefix="dnn" TagName="LANGUAGE" Src="~/Admin/Skins/Language.ascx" %>

Recompiling old ASP.NET page .dll's from new VB Codebehind?

I'm the newly appointed tech-guy at a small company. I've inherited their near-complete ASP.NET web page that's been left "near-complete" for some time, but I'm not really familiar with ASP.NET. It was dropped by the original developers a few years ago, and a year or so back someone tried to finish it using only inline code.
Now that I've unearthed the old VB source files, I've made a one-line change to the Page.aspx.vb and need get it associated to it's Page.aspx file. Trouble is, the #Page directive directly inherits its own App_Web_Page.xxxxxx.dll. Every page does this. This is not reflected in the source I have to work with, but is the way it now operates. The last person to touch it has long since forgotten the details of his work, and I'm not familiar enough with ASP.NET to know what to ask.
So my #Page directive goes like so:
<%# page language="vb" autoeventwireup="false" inherits="Page,
App_Web_page.aspx.cdcab7d2" masterpagefile="~/Master.Master"
enablesessionstate="True" enableEventValidation="false" %>
When I try to precompile the site, my #Page directives look totally different:
<%# page language="vb" autoeventwireup="false" Codebehind="Page.aspx.vb"
inherits="Project.Page" masterpagefile="~/Master.Master"
enablesessionstate="True" enableEventValidation="false" %>
And, being precompiled, It doesn't yield a page-unique .dll file I can just swap into the live bin directory and change the #Page directive for. I've tried a number of permutations on the #Page directive to try to get it to compile the new VB code on access, but I always wind up with errors, the best of which tell me ASP elements in the .aspx aren't defined in the .aspx.vb.
Can someone point me in the right direction with this?
You have to select the option like below highlighted...

Dynamically registering controls in .NET

Can anyone tell me if I can dynamically set the file name when registering a user control please, for example:
<%# Register src="[file name]" tagname="WebUserControl" tagprefix="uc1" %>
No, I don't believe you can. What you can do is register all of the possible controls that you might use on the page, either in the page directive or the web.config.
EDIT
What you can do, if this helps, is to add the controls dynamically in code-behind using the LoadControl method. This way, you can create instances of whatever user controls you want without worrying about registering them in the page directive or web.config. Thanks #Gabriel for pointing this out.

ASP.NET user control fails to render if registered with <%# Register Assembly="" (as opposed to Src="")

I'm trying to create a user control and reference it on an aspx page. It works, but only if I reference the individual control, like this:
<%# Register TagPrefix="schmapp" TagName="TestControl" Src="~/TestControl.ascx" %>
instead of referencing the whole namespace, like this:
<%# Register Assembly="UserControlTest" Namespace="UserControlTest" TagPrefix="app" %> <!-- doesn't work-->
I've done this many times yet it's been a while so I might be forgetting something basic. I refreshed my memory with a few tutorials and I think I'm doing the same steps.
I had this in the project I'm working on (.NET 3.5 under VS 2008) and I reproduced it step by step in a very basic project (.NET 4 under VS 2010) - I uploaded it for reference purposes.
I create a new asp.net web project, then Add -> New Item -> Web User Control, and then type some text into the ascx file (just to check if the control is being rendered). I then register the control and try to add it on the page. It works if it's referenced by Src attribute:
<schmapp:TestControl runat="server"/>
but not if the whole assembly is being referenced:
<app:TestControl runat="server" />
Now I can list the controls one by one but it's ugly and I don't want to accept defeat by something so simple, so I summon the might powers of teh internets to help.

Strongly-typed ASCX in WebForms 3.5?

I'm looking to get rid of the code-behind for a control in my WebForms 3.5 application. Again bitten by the bug of how it's done in MVC, I'd like to get a step closer to this methodology by doing:
<%# Control Language="C#" Inherits="Core.DataTemplate<Models.NewsArticle>" %>
This gives me the parser error you'd expect, so I remembered back to when this was an issue awaiting a fix in the MVC Preview, and changed it to:
<%# Control Language="C#" Inherits="Core.DataTemplate`1[[Models.NewsArticle]]" %>
But this doesn't work either! How is it that the MVC team were able to harness this ability? Was it something special about the MVC project type rather than the latest VS2008 Service Pack?
Short of giving up and requiring future templates to have code-behind files, what are my best options to get this as close to the generic user control method as possible?
Well, it appears like I've managed to do it. After looking at the PageParserFilter implemented by the MVC team for ViewUserControl<T>, I was able to construct something similar for my own DataTemplate<T> purposes. Sweet. I can now use the line:
<%# Control Language="C#" Inherits="Core.DataTemplate<Models.NewsArticle>" %>
And, without any code behind file, it parses! I'll report back if I find that I've broken something else in the process!
With WebForms you lose pretty much everything that makes them useful without a code behind page, because then VS can't auto generate the designer file that holds the actual definitions for all your runat="server" controls.
What you can do is have a common base page class, and make that generic:
public class DataTemplate<T> : Page {
public T Model {get;set;}
}
public partial class MyCodeBehindClass :
DataTemplate<Models.NewsArticle> {
...
}
This would allow all the drag-drop component stuff that WebForms does to work unhindered, while also allowing you to access a strongly typed model on the page:
<%# Control Language="C#" Inherits="MyCodeBehindClass" %>
<% foreach( var item in Model ) { %>
<!-- do stuff -->
<% } %>

Resources