Click here to Skip to main content
15,921,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating custom server control. I have two classes in project.
One is main class that render control and another will be used to render content based on condition.

I just want to know how to render content from other classes in main class.
For example. This is just an example.
My main class code:
C#
[ParseChildren(true, "TopLeft_Pane")]
   [DefaultProperty("InnerHTML")]
   [ToolboxData("<{0}:MyControl runat=server>")]
   public class Class1 : WebControl
   {
   [Bindable(true)]
       [Category("Appearance")]
       [DefaultValue("")]
       [Localizable(true)]
       public string InnerHTML
       {
           get
           {
               String s = (String)ViewState["Text"];
               return ((s == null) ? "[" + this.ID + "]" : s);
           }

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

   /* TopLeftPane is my another class and I create an object here */
   private TopLeftPane _topleftpane;
       public TopLeftPane TopLeft_Pane
       {
           get
           {
               if (_topleftpane == null)
                   _topleftpane = new TopLeftPane();
               return _topleftpane;
           }
       }

   protected override void CreateChildControls()
       {
           this.Controls.Add(TopLeft_Pane);
       }

   protected override void Render(HtmlTextWriter writer)
       {
           AddAttributesToRender(writer);

           writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding,
               "1", false);
       /* Begin Table */
           writer.RenderBeginTag(HtmlTextWriterTag.Table);

       /* Begin TD */
           writer.RenderBeginTag(HtmlTextWriterTag.Tr);

           /* Begin TD */
           writer.RenderBeginTag(HtmlTextWriterTag.Td);

           /* Rendering content from TopLeftPane class*/
       TopLeft_Pane.RenderControl(writer)

       writer.RenderEndTag();
       /* End TD */

       writer.RenderEndTag();
       /* End TR */

       writer.RenderEndTag();
       /* End Table */
   }

   }

   My TopLeftPane class

   [ParseChildren(true)]
   [PersistChildren(false)]
   public class TopLeftPane : WebControl
   {
       public TopLeftPane(): base(HtmlTextWriterTag.Div)
       {
       }
       private string _headertitle;
       public string HeaderTitile
       {
           get { EnsureChildControls(); return _headertitle; }
           set { EnsureChildControls(); _headertitle = value; }
       }
       protected override void CreateChildControls()
       {
           Controls.Add(new LiteralControl(HeaderTitile));
       }
       protected override void Render(HtmlTextWriter writer)
       {
           writer.Write(this.HeaderTitile);
       }
   }

My page code on page load event:
C#
MyControl.TopLeftPane top = new MyControl.TopLeftPane();
top.HeaderTitile = "Header title";


When I run the page, Header title always getting null.

This is a just sample code. I will have more than 20 classes so I dont want to write a code in main class to render whole control.

I hope all of you understand my problem. Any articles or suggetion would be greatly appreciate.
Posted
Updated 17-May-10 3:54am
v2

1 solution

Hi,

Your TopLeftPane control doesn't need the literal control:

C#
[ParseChildren(true)]
   [PersistChildren(false)]
   public class TopLeftPane : WebControl
   {
       public TopLeftPane(): base(HtmlTextWriterTag.Div)
       {
       }

       private string _headertitle;
       public string HeaderTitile
       {
           get {  return _headertitle; }
           set {  _headertitle = value; }
       }
     
       protected override void Render(HtmlTextWriter writer)
       {
           writer.Write(this.HeaderTitile);
       }
   }


In the page surely your text should be along the lines of:

C#
MyControl1.EnsureChildControls(); // makesure MyControl Instance has created sub control
MyControl1.TopLeft_Pane="Header title";
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900