Source code for LayoutMan web control

<%@ Register TagPrefix="UVc" Namespace="Asp2Aspx" Assembly="Asp2Aspx" %>

<%@ Page Language="C#" %>

<!DOCTYPEHTMLPUBLIC"-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML>
      < HEAD >
            <title>LayoutMan by Kevin , Josh </title>
            < script runat ="server">
            private void Page_Load(object sender, System.EventArgs e)
            {
                  lbl.Text = "PAGE_LOAD(): Align is: " + uvcBox.Alignment + "<BR>" + uvcBox.TestMessage;
            }

            protected void btnSubmit_onClick(object sender, EventArgs e)
            {     
                  if (uvcBox.Alignment == LayoutMan.VerticalAlignment) {
                        uvcBox.Alignment = LayoutMan.HorizontalAlignment;
                 } else {
                        uvcBox.Alignment = LayoutMan.VerticalAlignment;
                  }
                  lbl.Text = "btnSubmit_onClick() Align is: " + uvcBox.Alignment + "<BR>" + uvcBox.TestMessage;
            }

 

            </ script >

      </ HEAD >

      < body >

            < form runat ="server" ID ="Form1">

                  < h2 >

                        Kevin &amp; Josh's LayoutMan Control (source code here)

                  </ h2 >

                  < ul >

                        < li >

                        Click day 12 in the first calendar.

                        < li >

                        Click day 12 in the second calendar, which is not contained within the

                        LayoutMan control.

                        < li >

                              Click the <strong>Swap My Alignment!</strong>

                        button. This will now lay out the LayoutMan control's contents horizontally.

                        < li >

                              Now, click that button again, and this time the LayoutMan's Calendar remembers

                              that it has day 12 selected.</li>

                  </ ul >

                  < p >

                        < asp:Label id ="lbl" runat ="server" />

                  </ p >

                  < p >

                        < UVc:LayoutMan id ="uvcBox" runat ="server">

                              <asp:Panelid="Panel1"runat="server"BackColor="LightGreen"BorderWidth="1">Hi!</asp:Panel>

                              <asp:Panelid="Panel2"runat="server"BackColor="LightGreen"BorderWidth="1">Testing!</asp:Panel>

                              < asp:Calendar id ="Calendar1" runat ="server"></ asp:Calendar >

                        </ UVc:LayoutMan >

                  </ p >

                  < p >

                        < asp:Button id ="btnSubmit" OnClick ="btnSubmit_onClick" runat ="server" Text ="Swap My Alignment!" />

                  </ p >

                  < BR >

                  < asp:calendar runat ="server" ID ="Calendar2" />

            </ form >

      </ body >

</HTML>

 

 

// (C)2001 Kevin

// Layoutman.aspx.cs

// This is only code for demonstration purposes

 

using System;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Collections;

using System.Web.SessionState;

using System.Collections.Specialized;

 

namespace Asp2Aspx

{

      public class LayoutMan : System.Web.UI.Control, INamingContainer

      {

            public const int VerticalAlignment = 0;

            public const int HorizontalAlignment = 1;

            private ArrayList m_arrParsedControls = new ArrayList();

            private Table m_tbl = null;

            public string TestMessage;         

            public int Alignment

            {

                  get

                  {

                        if(ViewState["Alignment"] == null) return VerticalAlignment;

                        return (int) ViewState["Alignment"];

                  }

                  set

                  {

                        ViewState["Alignment"] = value;

                  }

            }

                 

            protected override void AddParsedSubObject(Object obj)

            {

                  if (obj is LiteralControl == false)

                  {

                        m_arrParsedControls.Add(obj);

                  }

            }

 

            // OnInit and Render both must be called as shown below in order for calendar to retain state

            protected override void OnInit(EventArgs e)

            {

                  RenderLayout();

                  // Do not call base.OnInit or else we cause unwanted results (loss of state in calendar)

            }

           

            protected override void Render(System.Web.UI.HtmlTextWriter writer)

            {

                  RenderLayout();

                  base.Render(writer);

            }

 

            // This was of no help...

            //          protected override void CreateChildControls()

            //          {

            //                RenderLayout();

            //          }

 

 

            protected void RenderLayout()

            {

                  //protected override void OnPreRender(EventArgs e) {

                  TableRow trow = null;

                  TableCell tcell = null;

                  int n1 = Controls.Count;

 

                  switch(Alignment)

                  {

                        case VerticalAlignment:

                              Controls.Clear();

                              m_tbl = new Table();

                              m_tbl.BorderWidth = 5;             

                              m_tbl.BackColor = System.Drawing.Color.LightGray;

 

                              TestMessage = "Vertical Alignment = " + Alignment + " m_arrParsedControls.Count=" + m_arrParsedControls.Count + ", Control count is " + n1 + "<BR>";

                              foreach (object o in m_arrParsedControls)

                              {                      

                                    Control c = (Control) o;                             

                                    tcell = new TableCell();

                                    trow = new TableRow();

                                    string s = c.ClientID;

                                    TestMessage += " adding " + s;

                                    tcell.Controls.Add(c);

                                    trow.Cells.Add(tcell);

                                    m_tbl.Rows.Add(trow);

                              }

                              Controls.Add(m_tbl);

                              break;

 

                        case HorizontalAlignment:

                              Controls.Clear();

                              m_tbl = new Table();

                              m_tbl.BorderWidth = 5;             

                              m_tbl.BackColor = System.Drawing.Color.LightGray;

                              trow = new TableRow();

                              m_tbl.Rows.Add(trow);

 

                              TestMessage = "Horizontal Alignment = " + Alignment + " m_arrParsedControls.Count=" + m_arrParsedControls.Count + ", control count is " + n1 + "<BR>";

 

                              foreach (object o in m_arrParsedControls)

                              {                      

                                    Control c = new Control();

                                    c = (Control)  o;

                                    tcell = new TableCell();

                                    string s = c.ClientID;

                                    TestMessage += " adding " + s;

                                    tcell.Controls.Add(c);

                                    trow.Cells.Add(tcell);

                              }

                              Controls.Add(m_tbl);

                              break;

                        default:

                              // Never will happen...

                              TestMessage = "DEFAULT Alignment = " + Alignment + " m_arrParsedControls.Count=" + m_arrParsedControls.Count + ", control count is " + n1;

                              break;

                  }

                  Dispose(); // Clean up... not needed but why chance memory leaks?

            }

      }

}