Hi Thomas,
What I had in mind was a bit different. If I'm understanding you correctly
you want your control to only allow childs controls to be added to an
specific cell of a one-row table, is that correct? Currently in your code,
you're deriving from Table, this would allow a page developer to actually do
more than what I believe you want. He/she could add more rows, cells, etc;
is this still okay?
--
Victor Garcia Aprea
Microsoft MVP | ASP.NET
Looking for insights on ASP.NET? Read my blog:
http://obies.com/vga/blog.aspx
To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
and not by private mail.
Hi Victor,
I have now managed to but content into my cell as I wanted. The problem is
that I can't get the controls that are nested inside my composite control to
be displayed properly.
So my question is how do I loop through the nested controls and get them
displayed inside the table. Could you pinpoint me in the right direction and
maybe explain why the code below does not work as intended?
/Thomas
here is my code:
Custom Control:
using System;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace cc
{
/// <summary>
/// Summary description for WebCustomControl1.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:CustomTable1 runat=server></{0}:CustomTable1>")]
[ParseChildren(false)]
public class CustomTable : System.Web.UI.HtmlControls.HtmlTable,
INamingContainer
{
private string text;
private HtmlTableCell c2 = new HtmlTableCell();
[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public string Text
{
get
{
return text;
}
set
{
EnsureChildControls();
c2.InnerHtml = value;
}
}
protected override void CreateChildControls()
{
//string HtmlInsideCell2;
HtmlTableRow tblRow = new HtmlTableRow();
HtmlTableCell c1 = new HtmlTableCell();
c1.InnerHtml = "Cell1";
tblRow.Cells.Add(c1);
tblRow.Cells.Add(c2);
HtmlTableCell c3 = new HtmlTableCell();
c3.InnerHtml= "Cell3";
tblRow.Cells.Add(c3);
this.Rows.Add(tblRow);
//Controls.Add(tblRow);
}
protected override void AddParsedSubObject(Object obj)
{
//this is where the child objects are added to the cell#2
HtmlTable tbl = new HtmlTable();
if(obj.GetType() is HtmlTableRow)
{
HtmlTableRow r1 = (HtmlTableRow)obj;
tbl.Rows.Add(r1);
}
c2.Controls.Add(tbl);
}
/// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override void Render(HtmlTextWriter output)
{
base.Render(output);
}
}
}
my aspx page:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm2</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="
http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="WebForm2" method="post" runat="server">
<Custom:CustomTable id="ct1" runat="server">
<TR>
<TD>Hello World!</TD>
</TR>
</Custom:CustomTable>
</form>
</body>
</HTML>