User control

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

0


source to share


2 answers


First, create 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 create your second web control that contains and displays the first one:



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 show it:

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

      

+2


source


Thanks, it works. I was missing only one line -



innerControl.RenderControl (author);

0


source







All Articles