views:

337

answers:

2

I have an extremely simple control i'm trying to render which looks like this:

using System;
using System.Web;
using System.Web.UI;

namespace CORE.BusinessObjects.Web.Controls
{
    public class TestControl : Control
    {
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("Hello from TestControl!");
        }
    }
}

I'm calling the control in the following manner:

<%@ Register TagPrefix="Custom" 
             Namespace="CORE.BusinessObjects.Web.Controls" %>    
<Custom:TestControl ID="testControl" runat="server" Visible="true">
</Custom:TestControl>

Am i doing something wrong? I have also failed to run ANY control samples i found online. Nothing executes. I can only execute the constructor of the control. Every other method i tried to override like Render() or CreateChildControls() doesn't get executed.

Thanks.

EDIT: I forgot to mention that the control is included on a page with a Master page. The control actually runs fine in the Master page, but not outside of it.

A: 

Trying inheriting from CompositeControl or WebControl

Chris Herring
+3  A: 

You should try the following:

a. Inherit from the System.Web.UI.Control.WebControl class.

b. Make sure that you create a control library and your custom control library is compiled and referenced in the relevant web project. Optionally, add it to the toolbox and drag it onto the form from there. That should also add the reference in one step.

c. Make sure that your control declaration has the Assembly attribute which points to the name of the referenced control assembly:

<%@ Register TagPrefix="Custom" Assembly="CORE.BusinessObjects" Namespace="CORE.BusinessObjects.Web.Controls" %>
<Custom:TestControl ID="testControl" runat="server" Visible="true" />
Cerebrus