Question

I have created visual web part and copied custom web control which I'd like to use. My control looks as below:

namespace AppsListWebPart
{
    [ToolboxData("<{0}:CustomImageButton runat=\"server\"></{0}:CustomImageButton>")]
    public class CustomImageButton : LinkButton
    {

        public string ImageUrl { get; set; }

        /// <summary>
        /// This image will override any background mentioned in CSS. Use this in case you want to use ASP.net (non-CSS) style URLs (like ~/images/add.png)
        /// </summary>
        public string BackgroundImageUrl { get; set; }

        public override void RenderBeginTag(HtmlTextWriter writer)
        {

            if (!string.IsNullOrEmpty(this.BackgroundImageUrl))
            {
                base.Style.Add("background", "url('" + base.ResolveUrl(this.BackgroundImageUrl) + "‘) left center no-repeat");
            }
            base.Style.Add("text-decoration", "none");
            base.Style.Add("text-align", "center");
            base.RenderBeginTag(writer);

        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            writer.Write("<span style='vertical-align:top;'>");
            base.RenderContents(writer);
            writer.Write("</span>");
            writer.Write("<img border='0′ style='margin-top:2px;margin-left:3px' src='" + base.ResolveUrl(this.ImageUrl) + "‘ />");
        }

    }

}

In my webpart's ascx file, I try to register this web control by:

<%@ Register TagPrefix="spt" Namespace="AppsListWebPart" %>

Unfortunately, IntelliSense doesn't show any spt: tags and when I try to run my web part with this code written by myself:

<spt:CustomImageButton runat="server"></spt:CustomImageButton>

I get the error: Unknown server tag 'spt:CustomImageButton'.

How to perform correct custom web control registration?

Edit

I changed the registration line to:

<%@ Register TagPrefix="spt" Namespace="AppsListWebPart.AppsListVisualWebPart" Assembly="AppsListWebPart" %>

and changed my web control namespace to AppsListWebPart.AppsListVisualWebPart

and I'm getting now:

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

In editor thou, when I enter spt: I see now my AppsListVisualWebPartUserControl, but not CustomImageButton.

In webconfig I found AppsListWebPart assembly registered in safecontrols, but nothing more.

Was it helpful?

Solution

(Edit) Sorry, I have deleted my previous answer because I didn't noticed that you said that you have changed your control namespace to be the same used in the Register tag.

You said that the exact error is "Could not load file or assembly 'AppsListWebPart'"... could you try to change the AppsListWebPart string in your register comand to the assembly full name?

To be clearer:

<%@ Register TagPrefix="spt" Namespace="AppsListWebPart.AppsListVisualWebPart" Assembly="AppsListWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=aaaaaaaaaaaaaaaa" %> 

(change the token and version to reflect your deployment situation)

OTHER TIPS

I had the same problem as you. I created a custom web server control based on SPGridView and tries to add it to my Visual Web Control. I solved the problem with this 2 steps.

<%@ Register TagPrefix="cc1" Assembly="CustomSpGridView, Version=1.0.0.0, Culture=neutral, PublicKeyToken=562a273772bbe3d8"
Namespace=CustomControl" %>
  1. Add this to the page that I want to use the custom control.

  2. Double click on the Package folder, this will open up the package designer. Click on the advance button at the bottom. Refer to the screen shot below

enter image description here

Try this, cause this solved my problem.

Hope this helps.

I assume you are packaging this web part as a solution. If so, check the manifest file in your solution package. You can do this in visual studio 2010 by clicking opening up the package editor and clicking on the Manifest tab. If you don't see the new namespace there registered as a safe control, that is at least part of the problem. Make sure the solution package name is correctly named and that it appears in the manifest file.

The correct format to register a tagprefix is:

 <%@ Register Tagprefix="[tagName]" Namespace="[Namespace]" Assembly="[AssemblyName], Version=[Version], Culture=[Culture], PublicKeyToken=[PublicKeyToken]" %>

Below is the example:

  <%@ Register Tagprefix="wssawc" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

Please make sure your assembly is in GAC and try IISRESET or Application Pool Recycle. For security reasons, if you choose BIN deployment instead of GAC, Please ensure assembly is in the web application's /bin folder.

jjczopek, It seems it hasn't been registered properly. To be sure just check your webconfig and see if anything has been registered there. How are you deploying it is it through visual studio? Patrick

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top