ASP.NET: how to register control implemented in another assembly - asp.net

I have own control implemented in "CommonControls" assembly (the same namespace). It us 'Custom control' inherited from 'WebControl' class and implemented without ascx file.
It is necessary to use this control in "main" web site. Could you please advise how to register this control?
I know it should be something like this:
<%# Register Assembly="CommonControls" Namespace="CommonControls"
TagPrefix="uc" TagName="TopMenuControl" Src="..." %>
But what should I specify in the "Src" property?
Thanks.

It seems like I've found solution myself:
<%# Register Assembly="CommonControls" Namespace="CommonControls" TagPrefix="uc" %>
<uc:TopMenuControl class="container" runat="server" />
Src and TagName are just not required :)
But anyway, any thoughts (if you see any better solution) are welcome .

Related

Could not load type error in application page

I have created a application page for login functionality in sharepoint by following this post.
But this giving me an error at inheriting the page. I have folder structure and .aspx file like below.
Did I done any mistake while giving the link to the .cs file in the application page?
After I deployed into my site, it is giving me error like below.
I am trying to resolve it from 2 hrs, but unable to find the solution. Can any one suggest me the way to do it please!!
Try to add
<%# Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
before
<%# Page ... %>
Add $SharePoint.Project.AssemblyFullName$ to your Inherits attribute
Instead of <%# Page Language="C#" AutoEventWireup="true" CodeBehind="YourPage.aspx.cs" Inherits="YourNamespace.YourPage" %>
Use <%# Page Language="C#" AutoEventWireup="true" CodeBehind="YourPage.aspx.cs" Inherits="YourNamespace.YourPage,$SharePoint.Project.AssemblyFullName$" %>
This could also be happened when there are another reference to that assembly which didn't exist. To show the error, simply check Ha Doan answer.

could not create type XYZ.ashx

When I am trying to build my solution I am getting Could not create type Upload.ashx.I am pulling out my hair to solve this.Can anyone suggest me how to rectify this problem.And I don't know how to actually append the namespace to the class?
As I am having
Here is my code:
<%# WebHandler Language="vb" CodeBehind="Upload.ashx.vb" Class="Upload" %>
And this is my dir structure:
wwwroot/Test/Myproject
I don't think there is such a thing as an .ashx.vb file type.
Try changing your top line to:
<%# WebHandler Language="VB" Class="Upload" %>
and then just put your code behind code in the .ashx file
<%# WebHandler Language="VB" Class="Upload" %>
Imports System
Imports System.Web
Public Class Upload : Implements IHttpHandler
'Code here....
End Class
Alternatively, have you tried building your code?
See this similar question for more help:
Custom Http Handlers IIS7 and ASP.NET
I've been banging my head against the wall for a few hours over this error "could not create type", and it may not help your exact problem
I had split my codebehind across many partial classes, all called "restserver" apart from one which was called "restServer" (note the capital S) it was that small difference that was causing the error, I changed them all to the same case and it worked perfectly.
VS isn't very helpful when working with ASHX files.
I know this is an old question, but it is at the top of google at the moment, so for anyone looking: In a website project, the handler directive expects different parameters for websites and webapps. In this case (website) was looking for codeFile instead of (webapp) codeBehind:
<%# WebHandler Language="vb" CodeFile="Upload.ashx.vb" Class="Upload" %>
Add below tag
<%# Assembly Name="YourNameSpace.YourHandlerClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=90e3045b123af1c3" %>
above your WebHandler tag
<%# WebHandler Language="vb" CodeBehind="Upload.ashx.vb" Class="Upload" %>
Necromancing a thread but I just had the same issue. The suggestion for Assembly Name by #sagar-s didn't work for me. So I tried this and I got a good build (not sure yet about actual functionality) :
<%# Assembly Src="~/MyService.ashx.cs" %>
<%# WebHandler Language="C#" CodeBehind="MyService.ashx.cs" Class="Namespace.MyService" %>
The Src and Name attributes are mutually exclusive. The CodeBehind attribute and Src attribute are redundant. I haven't tried to see if Src will be adequate if CodeBehind is removed. No time to experiment at almost 3am here - or to read docs which may clarify such things. HTH

Include Content in .aspx masterpage

In my current project we have 5 different masterpages, there are some common elements in each and its really annoying making the change in all 5, it kind of defeats the point of masterpages.
I have tried having parent and child master pages but that caused other problems for a different day.
Is there a way to include dynamic content in a masterpage?
I'm looking for something similar to the php and coldfusion include().
You can put user controls (.ASCX) in your master pages. Is this what you were attempting to accomplish?
Like so...
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="WebForms.master.cs"
Inherits="Tunafish.Web.Views.Shared.WebForms" %>
<%# Register Src="~/Content/Controls/SiteNavigation.ascx" TagName="Nav"
TagPrefix="sc" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<asp:ContentPlaceHolder ID="HeadContent" runat="server" />
</head>
<body>
<sc:Nav runat="server" />
Have you looked into User Controls? .ascx
Custom web controls are the way to go:
http://weblogs.asp.net/scottgu/archive/2006/11/26/tip-trick-how-to-register-user-controls-and-custom-controls-in-web-config.aspx
As mxmissile and JMP suggested, user controls are the way to go, but you might want to be thorough in your usage of them. When you include the master page, make sure you add the following markup along with the page declaration:
<%# MasterType
virtualpath="~/myMasterPages/Master.master"
%>
This will allow you to call functions/objects in your master pages so you can make changes to controls or have access from the page itself to various other objects. I have a property in my base usercontrol class called "ParentForm" that is a reference to the page it sits in. For user controls in the master page, I ended up having the same property and in the setter of that property I translate it down to the user controls.
You could set the masterpages to inherit from a class that dynamically inserts content or script to the page OnPreRender in code. It may seem out there but I have had to use this method.

Register User Control Issue

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.

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