Harendra
Harendra

Reputation:

Custom control to be used in another custom control

I have a custom control that I need to use in another custom control. I have written all code at server side (no HTML). Can anyone tell me how to write below line of code in code behind using htmlTextWriter and how to register this control or how to write custom control within another where html is written from code behind?

<cc2:test id="test1" runat="server" marqueedirection="left" marqueeheight="25"
            marqueewidth="725" ShowImage="False" ShowTitle="False" ShowUrlUnderLine="True"></cc2:test

Upvotes: 0

Views: 257

Answers (2)

harendra
harendra

Reputation:

Thankx it works.. i was missing only one line--

innerControl.RenderControl(writer);

Upvotes: 0

Chris Fulstow
Chris Fulstow

Reputation: 41872

First, build a simple custom web control:

namespace My.Controls
{
    public class InnerControl : Control
    {
        protected override void Render(HtmlTextWriter writer)
        {
            writer.WriteLine("<h1>Inner Control</h1>");
        }
    }
}

Then build your second web control that contains and renders the first:

namespace My.Controls
{
    public class OuterControl : Control
    {
        protected override void Render(HtmlTextWriter writer)
        {
            writer.WriteLine("<h1>Outer Control</h1>");
            InnerControl innerControl = new InnerControl();
            innerControl.RenderControl(writer);
        }
    }
}

Finally, register the control on your page, and display it:

<%@ Register TagPrefix="c" Namespace="My.Controls" %>
<c:OuterControl runat="server" />

Upvotes: 2

Related Questions