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.
Related
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" %>
Is it possible to use MVC Helpers in webpages (cshtml) ?
I'm trying out some controls from infragistics, and would like to use the html helper method to create the grid (avoiding some javascript) but I can't seem to get any intellisense.
No, I'm unsure if it should work?
Update: I want to use the following line:
#Html.Infragistics().Grid(....
inside my MyWebPagesPage.cshtml
Thanks for any help
Larsi
You should be able to, just add
#using infrajistics.namespace
You could also add the namespace to ur web.config namespaces section so that you don't have to add the #using in each view
There are no helper method that would render Infragistics controls. Please take a look at this page, it has detailed instructions of using Infragistics controls in MVC pages.
There are some limitations though. Infragistics doesn't have separate controls for MVC, they are simply making their asp.net controls available, but there's a drawback.
As long as you focus on areas of the controls that do not initiate post backs or rely on ViewState, you soon find many behaviors and functions that work perfectly in an ASP.NET MVC application.
Sample usage would be:
<%# Register Assembly="Infragistics.Web.Mvc" Namespace="Infragistics.Web.Mvc" TagPrefix="cc1" %>
<%# Register Assembly="Infragistics35.Web.v9.1, Version=9.1.20091.1015, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb"
Namespace="Infragistics.Web.UI.GridControls" TagPrefix="ig" %>
<ig:WebDataGrid ID="wdg"
runat="server" Width="50%"
EnableViewState="false">
</ig:WebDataGrid>
No MVC helpers involved.
I have a UserControl that I need to add dynamically. I tried to follow this MSDN article, but I'm not having any success....
http://msdn.microsoft.com/en-us/library/c0az2h86.aspx
The UserControl is basically an image gallery, and it loads some pictures based on an ID. My idea was to make this ID available as a property. Then when I create an instance of the control, I could set this ID and add it to the form.
I added a reference to the control in the .aspx page that will use it, like this:
<%# Reference Control="~/PictureGallery.ascx" %>
And in the UserControl I added a ClassName like this:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="PictureGallery.ascx.cs"
Inherits="PictureGallery" ClassName="PictureGallery" %>
When I try to create an instance in the .aspx.cs like the article suggests, Dim gallery As ASP.PictureGallery, I get an "Type ASP.PictureGallery is not defined".
The article mentions a namespace, ASP, and I tried importing it to the .aspx.cs with no luck.
So, I'm not able to get a reference to the UserControl.
How can it be fixed?
It sounds like you are confusing two separate ways of working with a UserControl.
One way is to register the control on your page so that you can put it into the page at Design Time e.g.
<div>
<asp:PictureGallery id="myGallery" runat="server" MyProperty="SomeValue">
</asp:PictureGallery>
</div>
The second is programmatically (or dynamically) adding it into the page at runtime in your code behind. If so, then you need to use the LoadControl function which is mentioned in the sample. You do not need to register the control in the aspx file if you do this.
e.g.
Dim gallery as PictureGallery = LoadControl("~/PathToControl/gallery.ascx")
gallery.MyProperty = "SomeValue"
placeHolder.controls.add(gallery)
edit
What is the class name of the control in the code behind...something like this:
Partial Public Class MyControlsClassName
Inherits System.Web.UI.UserControl
That is the type you need to use when you declare it. Is it within a namespace perhaps?
I don't think you've placed the control in your code behind. You may well have created the reference, but do you have a tag such as <asp:PictureGalary id="gallary"></asp:PictureGalary> anywhere in your aspx?
The ASP namespace is generated at run time- user controls get "compiled" as they are used by .aspx pages so this is why you get the error message "Type ASP.PictureGallery is not defined".
When adding user controls dynamically you should use the Page.LoadControl method:
Page.LoadControl("~/PictureGallery.ascx")
I'm working with referencing user controls on my ASPX page and I'm wondering what the difference is between these two page directives.
#Reference
#Register
#Register is primarily used for registering tag prefixes to declaratively use controls within a page.
<%# Register tagprefix="my" namespace="MyNamespace" %>
<my:CustomControl runat=server />
#Reference is primarily used to refer to a page or user control (by file name or virtual path) to programatically refer to members of the page or control.
<%# Reference Control="MyControl.ascx" %>
<% MyControl ctrl = (MyControl) Page.LoadControl("MyControl.ascx");
ctrl.CustomProperty = "..."; //REFERENCE directive is needed to access property
%>
#Register is the more commonly used directive. You use this when you want to use a user control in your aspx or ascx page declaratively. #Register associates the control with a specific prefix and you can then use it in your markup.
#Reference only tells ASP.NET to compile the other control when your aspx or ascx page is compiled. That makes sure it is available at run-time and can be added to your control hierarchy programmatically. This is less common since dynamically changing user controls at runtime is not comon.
Here's a good blog post about it.
http://weblogs.asp.net/johnkatsiotis/archive/2008/08/13/the-reference-directive.aspx
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.