I made a fromshell. Its a class that inherits from CompositeControl. This is how I use my class:
<custom:FormShell ID="asdf" Header="My Form Shell" runat="server">
<custom:Item runat="server">
<p>This is the content of the form.</p>
<p>Can put any HTML or ASP controls here</p>
</custom:Item>
</custom:FormShell>
This is what it renders:
The problem is, I want to be able to use my window with this code:
<custom:FormShell ID="asdf" Header="My Form Shell" runat="server">
<p>This is the content of the form.</p>
<p>Can put any HTML or ASP controls here</p>
</custom:FormShell>
//e.i. This class does not use the Item class. //Content goes directly into the FormShell (as opposed to the example above that I am currently using)
My Question is, How can I get access to the item inner controls and html of my form in code behind, without putting it in a Item class first. Here is my FormShell.cs which hold the Item class and FormShell Class:
using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace myControls
{
//Item class that will hold the stuff to render in the formshell (this is what I want to get rid of)
public class Item : Control { }
[ParseChildren(true, "Items")]
public class FormShell : CompositeControl
{
protected const int SIDE_CELL_WIDTH = 30;
protected const string BACKGROUND_COLOR = "#d3d3d3";
protected const string HEADER_BACKGROUND_IMAGE = "url('http://www.marcrenaud.net/_static/_other/greypopup/MiddleUp.png')";
protected string[] CORNER_IMAGE = new string[4]
{
"http://www.marcrenaud.net/_static/_other/greypopup/CornerUpLeft.png",
"http://www.marcrenaud.net/_static/_other/greypopup/CornerUpRight2.png",
"http://www.marcrenaud.net/_static/_other/greypopup/CornerDownLeft.png",
"http://www.marcrenaud.net/_static/_other/greypopup/CornerDownRight.png"
};
//List of item classes (I want to get rid of this)
private ArrayList m_items = new ArrayList();
protected Table m_table = new Table();
protected TableRow[] m_row = new TableRow[3];
protected TableCell[,] m_cell = new TableCell[3, 3];
protected Label m_header = new Label();
/////////////////////////////// Look at this code /////////////////////////////////
protected void AddContent()
{
//I want to get rid of the Items class. Right now, i only figured out how to
//Put content into my Form Shell class via Item class.
//Is there something similar to this.controls.childrenDeclaredOnPresentationPage or something ?
m_cell[1, 1].Controls.Add((Control)m_items[0]);
}
[Browsable(false)]
public ArrayList Items
{
get { return m_items; }
}
//////////////////////////////////Look at this code end//////////////////////////////
protected override void CreateChildControls()
{
InitializeTable();
InitializeControlStructure();
InitializeTableStyles();
InitializeCorners();
InitializeCssStyles();
AddContent();
this.Controls.Add(m_table);
}
protected void InitializeTable()
{
//Init the rows and cells
for (int i = 0; i < 3; i++)
{
m_row[i] = new TableRow();
for (int j = 0; j < 3; j++)
{
m_cell[i, j] = new TableCell();
}
}
}
protected void InitializeControlStructure()
{
m_header.Text = Header;
m_cell[0, 1].Controls.Add(m_header);
m_table.Controls.Add(m_row[0]);
m_table.Controls.Add(m_row[1]);
m_table.Controls.Add(m_row[2]);
m_row[0].Controls.Add(m_cell[0, 0]);
m_row[0].Controls.Add(m_cell[0, 1]);
m_row[0].Controls.Add(m_cell[0, 2]);
m_row[1].Controls.Add(m_cell[1, 0]);
m_row[1].Controls.Add(m_cell[1, 1]);
m_row[1].Controls.Add(m_cell[1, 2]);
m_row[2].Controls.Add(m_cell[2, 0]);
m_row[2].Controls.Add(m_cell[2, 1]);
m_row[2].Controls.Add(m_cell[2, 2]);
}
protected void InitializeTableStyles()
{
m_table.CellPadding = 0;
m_table.CellSpacing = 0;
m_cell[0, 0].Width = SIDE_CELL_WIDTH;
m_cell[0, 2].Width = SIDE_CELL_WIDTH;
m_cell[1, 0].Width = SIDE_CELL_WIDTH;
m_cell[1, 2].Width = SIDE_CELL_WIDTH;
m_cell[2, 0].Width = SIDE_CELL_WIDTH;
m_cell[2, 2].Width = SIDE_CELL_WIDTH;
m_cell[1, 0].BackColor = System.Drawing.ColorTranslator.FromHtml(BACKGROUND_COLOR);
m_cell[1, 1].BackColor = System.Drawing.ColorTranslator.FromHtml(BACKGROUND_COLOR);
m_cell[1, 2].BackColor = System.Drawing.ColorTranslator.FromHtml(BACKGROUND_COLOR);
m_cell[2, 1].BackColor = System.Drawing.ColorTranslator.FromHtml(BACKGROUND_COLOR);
}
protected void InitializeCorners()
{
Image[] corner = new Image[4];
for (int i = 0; i < 4; i++)
{
corner[i] = new Image();
corner[i].GenerateEmptyAlternateText = true;
corner[i].ImageUrl = CORNER_IMAGE[i];
}
m_cell[0, 0].Controls.Add(corner[0]);
m_cell[0, 2].Controls.Add(corner[1]);
m_cell[2, 0].Controls.Add(corner[2]);
m_cell[2, 2].Controls.Add(corner[3]);
}
protected void InitializeCssStyles()
{
//Initialize the table
m_table.Style["font-family"] = "Arial";
//Initialize the header
m_cell[0, 1].Style["background-image"] = HEADER_BACKGROUND_IMAGE;
m_cell[0, 1].Style["color"] = "White";
m_cell[0, 1].Style["text-align"] = "center";
m_cell[0, 1].Style["font-weight"] = "bold";
m_cell[0, 1].Style["font-size"] = "16px";
//Initialize the content area
m_cell[1, 1].Style["font-size"] = "14px";
}
public string Header
{
get;
set;
}
}
}
Marquis_DeBl...
Member
67 Points
51 Posts
How to get the inner text of a class that inherits from CompositeControl
Jul 04, 2011 08:48 PM|LINK
I made a fromshell. Its a class that inherits from CompositeControl. This is how I use my class:
<custom:FormShell ID="asdf" Header="My Form Shell" runat="server"> <custom:Item runat="server"> <p>This is the content of the form.</p> <p>Can put any HTML or ASP controls here</p> </custom:Item> </custom:FormShell>This is what it renders:
The problem is, I want to be able to use my window with this code:
<custom:FormShell ID="asdf" Header="My Form Shell" runat="server"> <p>This is the content of the form.</p> <p>Can put any HTML or ASP controls here</p> </custom:FormShell>
//e.i. This class does not use the Item class.
//Content goes directly into the FormShell (as opposed to the example above that I am currently using)
My Question is, How can I get access to the item inner controls and html of my form in code behind, without putting it in a Item class first. Here is my FormShell.cs which hold the Item class and FormShell Class:
using System; using System.Collections; using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel; namespace myControls { //Item class that will hold the stuff to render in the formshell (this is what I want to get rid of) public class Item : Control { } [ParseChildren(true, "Items")] public class FormShell : CompositeControl { protected const int SIDE_CELL_WIDTH = 30; protected const string BACKGROUND_COLOR = "#d3d3d3"; protected const string HEADER_BACKGROUND_IMAGE = "url('http://www.marcrenaud.net/_static/_other/greypopup/MiddleUp.png')"; protected string[] CORNER_IMAGE = new string[4] { "http://www.marcrenaud.net/_static/_other/greypopup/CornerUpLeft.png", "http://www.marcrenaud.net/_static/_other/greypopup/CornerUpRight2.png", "http://www.marcrenaud.net/_static/_other/greypopup/CornerDownLeft.png", "http://www.marcrenaud.net/_static/_other/greypopup/CornerDownRight.png" }; //List of item classes (I want to get rid of this) private ArrayList m_items = new ArrayList(); protected Table m_table = new Table(); protected TableRow[] m_row = new TableRow[3]; protected TableCell[,] m_cell = new TableCell[3, 3]; protected Label m_header = new Label(); /////////////////////////////// Look at this code ///////////////////////////////// protected void AddContent() { //I want to get rid of the Items class. Right now, i only figured out how to //Put content into my Form Shell class via Item class. //Is there something similar to this.controls.childrenDeclaredOnPresentationPage or something ? m_cell[1, 1].Controls.Add((Control)m_items[0]); } [Browsable(false)] public ArrayList Items { get { return m_items; } } //////////////////////////////////Look at this code end////////////////////////////// protected override void CreateChildControls() { InitializeTable(); InitializeControlStructure(); InitializeTableStyles(); InitializeCorners(); InitializeCssStyles(); AddContent(); this.Controls.Add(m_table); } protected void InitializeTable() { //Init the rows and cells for (int i = 0; i < 3; i++) { m_row[i] = new TableRow(); for (int j = 0; j < 3; j++) { m_cell[i, j] = new TableCell(); } } } protected void InitializeControlStructure() { m_header.Text = Header; m_cell[0, 1].Controls.Add(m_header); m_table.Controls.Add(m_row[0]); m_table.Controls.Add(m_row[1]); m_table.Controls.Add(m_row[2]); m_row[0].Controls.Add(m_cell[0, 0]); m_row[0].Controls.Add(m_cell[0, 1]); m_row[0].Controls.Add(m_cell[0, 2]); m_row[1].Controls.Add(m_cell[1, 0]); m_row[1].Controls.Add(m_cell[1, 1]); m_row[1].Controls.Add(m_cell[1, 2]); m_row[2].Controls.Add(m_cell[2, 0]); m_row[2].Controls.Add(m_cell[2, 1]); m_row[2].Controls.Add(m_cell[2, 2]); } protected void InitializeTableStyles() { m_table.CellPadding = 0; m_table.CellSpacing = 0; m_cell[0, 0].Width = SIDE_CELL_WIDTH; m_cell[0, 2].Width = SIDE_CELL_WIDTH; m_cell[1, 0].Width = SIDE_CELL_WIDTH; m_cell[1, 2].Width = SIDE_CELL_WIDTH; m_cell[2, 0].Width = SIDE_CELL_WIDTH; m_cell[2, 2].Width = SIDE_CELL_WIDTH; m_cell[1, 0].BackColor = System.Drawing.ColorTranslator.FromHtml(BACKGROUND_COLOR); m_cell[1, 1].BackColor = System.Drawing.ColorTranslator.FromHtml(BACKGROUND_COLOR); m_cell[1, 2].BackColor = System.Drawing.ColorTranslator.FromHtml(BACKGROUND_COLOR); m_cell[2, 1].BackColor = System.Drawing.ColorTranslator.FromHtml(BACKGROUND_COLOR); } protected void InitializeCorners() { Image[] corner = new Image[4]; for (int i = 0; i < 4; i++) { corner[i] = new Image(); corner[i].GenerateEmptyAlternateText = true; corner[i].ImageUrl = CORNER_IMAGE[i]; } m_cell[0, 0].Controls.Add(corner[0]); m_cell[0, 2].Controls.Add(corner[1]); m_cell[2, 0].Controls.Add(corner[2]); m_cell[2, 2].Controls.Add(corner[3]); } protected void InitializeCssStyles() { //Initialize the table m_table.Style["font-family"] = "Arial"; //Initialize the header m_cell[0, 1].Style["background-image"] = HEADER_BACKGROUND_IMAGE; m_cell[0, 1].Style["color"] = "White"; m_cell[0, 1].Style["text-align"] = "center"; m_cell[0, 1].Style["font-weight"] = "bold"; m_cell[0, 1].Style["font-size"] = "16px"; //Initialize the content area m_cell[1, 1].Style["font-size"] = "14px"; } public string Header { get; set; } } }