LoadControl on control with assembly reference - asp.net

I'm trying to use System.Web.UI.TemplateControl.LoadControl to build a templated web part for a SharePoint application, and I'm having trouble with a control that references an external assembly.
I have a user control with the first few lines like this:
<%# Control Language="C#" ClassName="MyControl" %>
<%# Assembly Name="AjaxControlToolkit" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
The web part derives from a base webpart that implements the templating and some other utilities which is in an assembly called "MyWebParts" (for example), while the derived web part class is in "MyProject". MyProject has a reference to "MyWebParts" (obviously), and to "AjaxControlToolkit", but "MyWebParts" does not have a reference to "AjaxControlToolkit". At runtime, when the templated web part tries to load the user control (a .ascx file in the "controltemplates" SharePoint root folder), it calls the base methods it inherited across assemblies, and it throws an HttpParseException, indicating that it couldn't load a required assembly. The EventViewer confirms that the assembly in question is AjaxControlToolkit.
I tried adding the AjaxControlToolkit assembly to the web.config for sharepoint, and it's sitting in the GAC, but to no avail.
Anyone know how I can do this without having a reference to every assembly in existence in my "MyWebParts" assembly?

Add a reference to your assembly in the web.config assemblies section, this will force sharepoint to load that assembly into memory, like so:
<assemblies>
<add assembly="AjaxControlToolkit, Version=1.0.10920.16768, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"/>
</assemblies>
P.S. this is for ajax 1.0, correct the assembly version if you are using the AJAX 3.5 version.

Related

Add Assembly (in Web.Config) required for assembly in GAC

Please see the code below (Image Hander):
<%# WebHandler Language="VB" Class="com.Genie.PresentationLayer.Web.ImageHandler1" %>
Imports com.Genie.BusinessLogicLayer
Namespace com.Genie.PresentationLayer.Web
Public Class ImageHandler1
Inherits ImageHandler
Private p1 as Person
Public Sub New()
End Sub
end class
end namespace
com.Genie.BusinessLogicLayer was added to the GAC today. The application produces a runtime error when it gets to the Image Handler: 'Type Person is not defined'. Adding an assembly to the web.config as follows resolves the problem:
<assemblies>
<add assembly="BusinessLogicLayer, Version=1.0.0.0, Culture=neutral, PublicKeyToken="669e0ddf0bb1aa2a"/>
</assemblies>
Why do I have to amend the web.config? I thought 'Add Assembly' was for website projects that do not use MSBuild (and therefore do not have 'References'). This is a web application project.
The references you add to a project are used when the project is compiled, but an ASPX page or ASHX handler is compiled when it is first requested. At this time the references are lost, so you'll need to provide them yourself.
The alternative would be precompiling your ASPX pages, causing the error to be caught at compiletime as opposed to runtime, or moving the code to codebehind so your project's references will be used.

Could not load file or assembly 'AjaxControlToolkit' or one of its dependencies. The system cannot find the file specified

I am working on sharepoint 2010. I have created a simple visual web part which contain one text box and I have added calendarextender to it. I have added ajaxcontroltoolkit.dll as reference. But when I deploy to sharepoint site then it gives me following error.
"Parser Error" Message:
Could not load file or assembly 'AjaxControlToolkit' or one of its dependencies. The system cannot find the file specified.
Source Error:
Line 3: <%# Register Assembly="AjaxControlToolkit"> Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
Source File:
/_CONTROLTEMPLATES/VWP_Ajax/VisualWebPart1/VisualWebPart1UserControl.ascx Line: 9
Any ideas where Im going wrong?
Is the AjaxControlToolkit.dll in the bin directory after you deploy the site?
The assembly (AjaxControlToolkit.dll) needs to be deployed to the Global Assembly Cache (GAC) or put in the \BIN folder of the SharePoint site's folder.
This definitely worked for me with SharePoint 2010 ...
Error Code:
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxControlToolkit"%>
Working Code:
<%# Register Assembly="AjaxControlToolkit, Version=3.5.60501.0, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e" Namespace="AjaxControlToolkit" TagPrefix="ajaxControlToolkit"%>
Another possible solution is to explicitly set the full assembly name in your register command. I found that I had to update my code to include this to get it to work properly:
<%# Register Assembly="AjaxControlToolkit, Version=3.0.30930.28736, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
For third-party controls like this, it's worth being as specific as possible to avoid any errors if versions change.
Even you tried above and it wont work, Just add another Page/MasterPage and add ToolKitScriptManager on it.It will create Bin Folder at required path. If still there is Error check the Register Assembly line on newly added page/MasterPage and copy that Register Assembly line to other pages wherever your error is.

Custom Control in ASP.NET C#

I created a simple custom control that only inherits from the Literal control, and doesn't have any extensions yet, code is empty.
Namespace: CustomControls
Class name: Literal : System.Web.UI.WebControls.Literal
Next thing I do is registering this control in the aspx page as following:
<%# Register TagPrefix="web" Namespace="CustomControls" %>
(I read in few tutorials that this is one of the ways to register it, besides web.config etc.)
After all, no intellisence for me, and worse- I get a parse error 'unknown server tag: web' when I try to run the page with the control in it.
I used 'create new project' and not new website, in case this info is needed.
What could be my problem?
Thanks in advance.
Here is how I did it, step by step starting from nothing. This first method uses a second project/assembly. For the App_code version scroll down.
Web Application Project Method
Create a new ASP.Net Web Application. Take note of the name, mine is called WebApplication2. If you already have an existing web application, which is likely, double click the properties section of your project and check the "Assembly Name" property, make note of it.
Create a new Class in the web applcation named Literal.cs
Put in code similar to the following for the class defenition:
namespace CustomControls
{
public class Literal : System.Web.UI.WebControls.Literal
{
}
}
Add the following register tag to the top of your aspx page
<%# Register assembly="WebApplication2" namespace="CustomControls" tagprefix="web" %>
If your assembly name was different then change it here. I noticed that when I did this in VB.Net the namespace was WebApplication1.CustomControls instead of just CustomControls like it was in C#, kind of odd.
Add the new control to your page:
<web:Literal ID="Literal1" runat="server" Text="test" />
Seperate Project Method
Create a new Empty Website (ASP.Net).
Add a new ASP.Net Server Control library named CustomControls to the solution.
Add a new Class to the new project called Literal (I'm using C# so my file is named Literal.cs). Below is my super basic code, that I believe should match the code described in the question.
namespace CustomControls
{
public class Literal : System.Web.UI.WebControls.Literal
{
}
}
Add a reference to the CustomControls project to your website.
Add the assembly registration to the top of your aspx page:
<%# Register assembly="CustomControls" namespace="CustomControls" tagprefix="web" %>
Add a new instance of the control to your page:
<web:Literal ID="Literal1" runat="server" Text="test" />
In App_Code Method
Create a new Empty Website (ASP.Net).
Add a new Class to the App_Code folder Literal2 (I'm using C# so my file is named Literal2.cs). Below is my super basic code, that I believe should match the code described in the question. I called it 2 so that you can use this in conjunction with the method described above without getting compile errors
namespace CustomControls
{
public class Literal2 : System.Web.UI.WebControls.Literal
{
}
}
Register the assembly/namespace for app_code in your aspx page by adding the following line to the top
<%# Register Namespace="CustomControls" Assembly="__code" tagprefix="web" %>
Add an instance of the new control to your page:
<web:Literal2 ID="literal2" runat="server" Text="test2" />
I tested this using visual studio and it all worked fine for me.
You can also register your control in web.config if you would like to use it on other pages
<pages>
<controls>
<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add tagPrefix="web" namespace="MyProject.CustomControls" assembly="MyProject" />
</controls>
</pages>
For web site this will work
<add tagPrefix ="web" namespace="ASP.App_Code.CustomControls" />
The problem lies in your <%# Register TagPrefix="web" Namespace="CustomControls" %> tag: it misses the assembly name!
It must become <%# Register TagPrefix="web" Namespace="CustomControls" Assembly="YourAssemblyName" %>. If you have difficulties finding the assembly name for your class here are a few hints:
If you created the class inside the web application's Visual Studio project, or in a separate DLL (shared library) project, then right-click the project and go to Properties to get the assembly name
If you created the .cs file in App_Code directory, ASP.App_Code should work
Otherwise please give us some information about project layout!
The easiest thing to do is to drag-and-drop the Literal.ascx on to the page (if the page is in the same solution) .If the page is another solution, you could add the control to VS toolbox and drag-and-drop the control on the page.
You forgot the src attribute.
<%# Register TagPrefix="web" Namespace="CustomControls" Src="PathToYourCustomControl" %>

How to integrate an external web ASP.NET control into a SharePoint application's page?

How to integrate an external web ASP.NET control into a SharePoint application's page?
What did you mean by External Website's control? You can use the page view webpart to load an external page/ Is this what you are looking for?
Have you seen this article about creating an Application page?
Essentially, inside your application page you can reference the DLL:
<%# Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"%>
Then register a tag prefix:
<%# Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
Then you can use your controls:
<SharePoint:EncodedLiteral runat="server" text="<%$Resources:wss,approve_pagetitle%>" EncodeMethod='HtmlEncode'/>
Instead of SharePoint, you'll be using the DLL that contains your ASP.NET web controls.

ASP.NET "Reference required to assembly" error

I have an ASP.NET page A that uses a data layer assembly DAL. I reference the DAL assembly with a
<%# Assembly Name="NGC.DAL" %> tag since there is no project to add the reference to. The assembly itself I copied to the pages' bin folder.
The assembly DAL is a project that references System and System.Data.
Now when A calls a method in DAL, the compiler gives the error
BC30652: Reference required to assembly 'System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=969db8053d3322ac, Retargetable=Yes' containing the type 'System.Data.IDbConnection'. Add one to your project.
I tried referencing the assembly System.Data with a <%#Import Namespace="System.Data" %> tag and I tried copying it to the bin folder and referencing it with a
<%# Assembly Name="System.Data" %> tag, both in A directly. Nothing worked.
Why would a project A that uses project B which uses project C require a reference to B and C rather than just B and why does the way above not work?
This MSDN thread covers the problem but shows no solution that works.
EDIT: As Robert Wagner suggested I added the reference to the web.config.
Now I get another error message. BC30560: 'CommandBehavior' is ambiguous in the namespace 'System.Data'. which is odd because the one in the web.config is the only one I reference inside the page.
Maybe it's worth mentioning that the System.Data referenced and used in the DAL assembly is from the compact framework. But since it's the only one I use, where does the ambiguity come from?
Most likely you have inherited from a System.Data class in your DAL dll and are using that class in your website. It requires a reference to be able to resolve the references those classes otherwise the compiler would not know what the class contract is.
You should not need to copy in System.Data as it is in the GAC.
A better way would be to add the reference in your web.config file:
<system.web>
<compilation debug="true">
<assemblies>
<add assembly="System.Data, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=B77A5C561934E089" />
</assemblies>
</compilation>
</system.web>
When you say "there is no project to add references to" are you using Visual Studio? If so, why not use the Web Application or Web Site projects?

Resources