Building a custom control that implement table server control

T

Thomas Ekegren

Hi,

I want to create a custom control that implements the table htmlcontrol. In
my webform page I want to include that custom control and add content to a
cell in my custom table control. I don't want to do this dynamically in the
page_load event but in the HTML design view.

Example:

My custom control containes
a table
one row
and three cells

_____________________________
| Cell1 | Cell2 | Cell3 |
_____________________________

I want to position my content in cell #2

<HTML>
<BODY>
<CustomControl:myTable id="myTable1" runat="server">

/**** this is what i want to position in cell #2 ****/
<tr>
<td>My text</td>
</tr>

</CustomControl:myTable>
</BODY>
</HTML>

Any suggestion as to how to accomplish this?

Thanks,

/ekegren
 
V

Victor Garcia Aprea [MVP]

Hi Thomas,

You could write a composite control that contains an HtmlTable control. This
composite control would allow the user to declaratively specify its child
controls, ie:

<cc:YourCompositeControl>
//contents for 2nd cell goes here
</cc:YourCompositeControl>

Then your composite control should create an HtmlTable, add one row and
three cells, and insert the specified child controls in the 2nd row.

You can find more info about composite controls and their keys method (ie.
CreateChildControls, etc) in the docs and in web tutorials. You will need
also to get familiar with attributes and the specific ones that control the
parsing and serialization of your control and its childs
(ParseChildrenAttribute, etc). There are lots of posts and samples posted in
this ng, you could use http://groups.google.com to find them and learn from
there.

Give that a try and post here any problems you encounter,


--
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.
 
V

Victor Garcia Aprea [MVP]

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top