Custom Control in ASP.NET C# - asp.net

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" %>

Related

How to add ajax control in the project

I am using asp.net using C#.I want to add Ajax Control to my Web Pages.I am using visual studio 2005.That had ajax basic control not extended control and bin folder contain ajax controltoolkit.dll.But when I am adding control to my page,it shows the unknown elements.I add references and select that file(dll).Still the assembly details is not coming in my aspx page.And still saying unknown element.I have another project also.In that I done same ,and it works also.
You'll need to edit your web.config
<add assembly="AjaxControlToolkit, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"/>
Goes between
<assemblies></assemblies>
tag.
<add tagPrefix="ajaxToolkit" namespace="AjaxControlToolkit" assembly="AjaxControlToolkit, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"/>
Goes between
<controls></controls>
tag.

Register custom class in assembly with web project

I'm using web project and web forms.
I create Custom Class
Namespace CustomWebControls
Public Class NoValidationDDL
Inherits DropDownList
End Class
End Namespace
and i try to register this class in assembly web.config page section
<controls>
<add tagPrefix="customDDL" namespace="CustomWebControls" assembly="ae" />
</controls>
and try to use in ascx
<customDDL:NoValidationDDL ID ="id" runat="server"></customDDL:NoValidationDDL>
How can i do this in web project?
This should work (assuming the assembly name for your project is ae) - make sure that you build the project after creating/modifying your custom control. Unless updated 'ae.dll' is present in your bin directory, intellisense etc will not work.
Instead of modify web.config, you may also try register control at page(aspx) or control(ascx) level by adding register directive after page directive:
<%# Register Assembly="ae" Namespace="CustomWebControls" TagPrefix="customDDL" %>

Install AJAX without web.config?

I have a weird scenario, I need to use the AJAX ScriptManager and UpdatePanel on two specific ASP.NET 2.0 pages. The pages are in their own second-level directory (we don't want to make that directory a Virtual Directory). The root web.config is not AJAX enabled, and we don't want to change it.
Is is possible to use AJAX here, and how?
I was hoping it might be as easy as :
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
Is it an option for you to add a "local" web.config file in the sub directory in question? If you do so, all tags that are not present in the "local" file will be read from the "root" file.
You need to add a reference to System.Web.Extensions.dll to the application; without that, I'm pretty sure it's completely impossible.
However, if you just add a reference to that assembly to the root Web.config (without changing anything else), you should be able to register the tag prefix in your ASPX files, like this:
<%# Register tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" %>
I haven't tried it, though.
Please note that if the server isn't running .Net 3.5, you'll need to copy
System.Web.Extensions.dll to the application's Bin folder.
I believe you can create a web.config in your target folder that overrides the settings of the global web.config in the application root.
See
http://www.codeproject.com/KB/aspnet/multipleWebConfig.aspx
Mind you, I haven't tried this with tag registration, so YMMV.

How do I add a reference to a server control in my current website to the web.config

I have extended a server control (not a user control) and put the code in my app_code folder.
I would like to add a tag prefix to the web config, but
<add tagPrefix="cc1" namespace="mynamespace" />
and
<add tagPrefix="cc1" namespace="mynamespace" assembly="currentwebsitename" />
don't work.
I get this error:
Error 147 Unknown server tag 'cc1:Control'
To register server controls that are in the App_Code folder, you only need the tag prefix and namespace. So in web.config it would look like this...
<add tagPrefix="cc1" namespace="mynamespace"/>
And in a page it would look like this...
<%# Register TagPrefix="cc1" Namespace="mynamespace" %>
One gotcha to watch out for is that by default web site projects don't include any namespace at all when you add a new item to the App_Code folder, so you'll need to explicitly make sure your controls have a namespace.
You either need to put the control into a DLL named "currentwebsitename.dll" (if you want it to work the second way) or you need to specify the source via the src attribute (if you want to do it the first way):
<add tagPrefix="cc1" namespace="mynamespace" src="app_code/control_name_here"/>
Try reading over these two articles as well:
http://msdn.microsoft.com/en-us/library/sbz9etab.aspx and
http://msdn.microsoft.com/en-us/library/yhzc935f.aspx

LoadControl on control with assembly reference

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.

Resources