How to add web user control to cell

J

Jeff User

Hi

Using C# in vs2003 to develop web stuff.

I created a "Web user control" (.ascx and ascx.cs files), that
contains a few simple (for starters) html items. I can place the item
on webform at development time and it works ok. What I want to do
however is be able to create an instance of a control and then add it
into the cell of a table row at run time. I cant seem to get this to
work. The Controls.Add method of the table cell requries a
System.Web.UI.Control object, but the user control I made is a
System.Web.UI.UserControl. Don't know if this is the problem or not.

This compiles and runs, but nothing shows up in the browser. Checking
the html sent to the browser, shows that there is nothing between the
<td id="cell_0_0" ></td> tags.

Here is a bit of what I was trying:

//Declare new instance of my user object
protected MyNamespace.header objHead;

//Get a reference to the table cell
System.Web.UI.WebControls.TableCell c =
(TableCell)Page.FindControl("cell_0_0");

//Create new instance of my object
MyNamespace.header objHead = new MyNamespace.header();

//Add the new object to the table cell
c.Controls.Add(objHead);


Thanks in advance
Jeff
 
G

Guest

instead of creating the control like such, try loading it like this:

objHead = (header )Page.LoadControl
("/UserControls/objHead.ascx");
this.Controls.Add(objHead);

replacing the folder userControls with what ever folder your ascx resides in.
 

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

Top