Include a usercontrol in a table

G

Guest

Hi,

This question is related with c# and ASP.NET, (I could't find the rigth
forum).

What I need to do is to add a usercontrol in a row of my table using the
server code (I know how to do it on the front-end). The idea is to show the
search results on the page, every entry is a usercontrol (obviously the same
loaded with different data). I have tried this but it does not work, any
ideas? (here is an example)

protected MyUserControl[] m_MyControls;
protected Table m_MyTable;
public void On_Load(...)
{
MyControls = new MyUserControl[2];
MyControls[0] = new MyUserControl();
MyControls[1] = new MyUserControl();

MyControl[0].Description = "Text1";
MyControl[1].Description = "Text2";

MyTable.Rows[0].Cells[0].Controls.Add(MyControl[0]);
MyTable.Rows[0].Cells[0].Controls.Add(MyControl[1]);
}

No errors are triggered, seems to be adding the controls but no ouput is
visible (none even using "View source").

Any Ideas?
Thanks
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,



Salvador said:
Hi,

This question is related with c# and ASP.NET, (I could't find the rigth
forum).

microsoft.public.dotnet.framework.aspnet will do
What I need to do is to add a usercontrol in a row of my table using the
server code (I know how to do it on the front-end). The idea is to show
the
search results on the page, every entry is a usercontrol (obviously the
same
loaded with different data). I have tried this but it does not work, any
ideas? (here is an example)

You can't do it like that, first of all what is wrong with doing it in the
aspx page? ( I assume you refer to this when you talk about the front-end) ?
I always do it like that, you have all the grid declaration in the aspx
page

In case you dont want to do it for some reason the solution is intercept the
DataGrid.ItemDataBound event. it does fire when the grid is binding the
datasource.


cheers,
 
G

Guest

Hi,

Thanks for the reply,
the problem that I have is that my user control is the entity, I don't know
how many I have to render until the page is processed (sorry, is my first
project in aspnet)

So, if I have it on the aspx page I will have (generic code)

<table>
<row>
<cell>
<MyHeader:MyUserControl Id = "MyName" runat ="server"/>
</cell>
</row>
</table>

This works great for a single instance but I want several, the result on
html has to be something like this:

<cell>
<MyHeader:MyUserControl Id = "MyName" runat ="server"/>
<MyHeader:MyUserControl Id = "MyNamex" runat ="server"/>
<MyHeader:MyUserControl Id = "MyNamexx" runat ="server"/>
<MyHeader:MyUserControl Id = "MyNamexxx" runat ="server"/>
</cell>

The amount of them is dynamic, that's why I am creating several instances of
the user control on the server code. To do an analogy, is like creating a
data repeater where the individual content is a usercontrol.

Hope this makes sense :)

Salva
 

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