views:

820

answers:

4

I am looking for a link to instructions or a walkthru for Creating Custom Controls in Asp.net 3.5.

I have already looked at the following :

http://forums.asp.net/t/1385382.aspx : Turning an .ascx User Control into a Redistributable Custom Control asp.net 3.5

http://msdn.microsoft.com/en-us/library/aa479318.aspx : Turning an .ascx User Control into a Redistributable Custom Control asp.net 2.0

I think the two above links are for Composite Custom Controls which would be fine for now, as it seems it is easier to make a Composite rather than a full Custom control.

As per the instructions in the above link ( aa479318 ) , I created a user control MyControl.ascx file and published it, which was compiled it into a self contained .dll which was named App_Web_MyControl.ascx.cdcab7d2.

Then I put a

<%Register Assembly="App_Web_MyControl.ascx.cdcab7d2" 
              TagPrefix="cc" namespace="TheNamespace" %> 

in the aspx file (in another app) where I wanted to use the Custom Control, and I add a reference to the .dll assembly in the project.

The CustomControl name is not being recognized when I try creating it in the .aspx code by

<cc:MyControl ID="idname" runat="server" />

I get error Element 'MyControl' is not a known element.'

A: 

Take a look at Scott's Post on Registration as this will allow you to register your UserControl, which you have converted into a custom control inside your web.config. This allows you to set the tag name of the control as well since the actual name of your class probably got mangled in the generated App_Web_MyControl.ascx.cdcab7d2 dll.

This hopefully allows you to do something like the following...

<?xml version="1.0"?>

<configuration>

  <system.web>

    <pages>
      <controls>
        <add tagPrefix="cc" assembly="App_Web_MyControl.ascx.cdcab7d2" tagName="MyControl"/>
      </controls>
    </pages>

  </system.web>

</configuration>

...thus sidestepping your class name "MyControl" issues.

The other option is to use the .Net Reflector to go inside your App_Web_MyControl.ascx.cdcab7d2 assembly (shouldn't have said dll earlier) and figure out what "MyControl" got changed to. This is a bit uglier since this generated name is likely to change anytime you recompile your App_Web_MyControl

Dave Jellison
The following resources may also point you in the right direction...http://stackoverflow.com/questions/935468/convert-user-controls-to-server-controlshttp://www.nathanblevins.com/Articles/Compile-a-Web-User-Control-into-a-DLL-.Net-c-.aspxhttp://www.codeproject.com/KB/aspnet/Usercontrol_to_WebControl.aspxhttp://bhaidar.net/cs/archive/2006/01/31/83.aspx
Dave Jellison
A: 

If I were you, I would start here:

Create a project of type "ASP.Net Server Control." It should create a class that looks like this (my class is called ServerControl):

[DefaultProperty("Text")]
[ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")]
public class ServerControl1 : WebControl
{
    [Bindable(true)]
    [Category("Appearance")]
    [DefaultValue("")]
    [Localizable(true)]
    public string Text
    {
        get
        {
            String s = (String)ViewState["Text"];
            return ((s == null) ? "[" + this.ID + "]" : s);
        }

        set
        {
            ViewState["Text"] = value;
        }
    }

    protected override void RenderContents(HtmlTextWriter output)
    {
        output.Write(Text);
    }
}

You can then add fields and properties and methods (public, private, or protected). If you don't want to override the RenderContents method, you can create a composite control by overriding the CreateChildControls method (see this example).

Look into the HtmlTextWriter class for more information on what to do in the RenderContents method above.

Gabriel McAdams
This is absolutely the correct way to create server controls rather than generating/reusing your UserControls. I'm under the assumption you absolutely had to do the UserControl manipulation for a specific purpose. If not, you should definitely use the above methodology to write your server controls.
Dave Jellison
+1  A: 

This is late, I know, but here goes..

I successfully turned an .ascx UserControl into a redistributable dll by doing this:

  1. Created an ASP.NET Web Site project called AscxDll
  2. Developed my UserControl in said project at this location: ~/App_UserControls/RadioButtonQuestion.ascx.
  3. Published the Website project with only these settings ticked in the Publish Web Site dialog:

    Use fixed name and single page assemblies

    Emit debug information

  4. Open VS cmd prompt and run: aspnet_merge "C:\PrecompiledWebOutputDir\AscxDll" -o AssemblyName -r
  5. Copied the merged AssemblyName.dll to bin folder of another webapp/website project that needed it.
  6. In client project's web.config, added this to pages\controls <add tagPrefix="MyPrefix" namespace="ASP" assembly="AssemblyName"/>
  7. Added control to a web page in client project: <MyPrefix:app_usercontrols_radiobuttonquestion_ascx ID="rbq1" runat="server" QuestionText="Set by aspx source" />

Note how it is registered in web.config using namespace="ASP", and how the tag on the web page has a few underscores in it <MyPrefix:app_usercontrols_radiobuttonquestion_ascx /> This may be the key to where you're going wrong?

This worked well for me, although it took me AGES to figure out. It's probably easier to just build a server control or composite control, unless your UI is very complex.

Hope this helps somebody.

antmx
Rabid
OK thanks, I should try that. Although I'm reluctant to reopen this particularly messy can of worms!
antmx
A: 

Most of the time developers register the User Controls to individual asp.net pages where, controls are in use. If the control is in use in most of the pages of the site, the reference can be added at one place only, that is in the web.config.

Code to add in web.config file to register the user control

Eliza

eliza sahoo