I am trying to create a module that displays some data in a table, with one data record per cell.
I am using the DataList control. If I try something like this:
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "StringValue") %>
</ItemTemplate>
Everything works fine, and I get my data in the cell.
However, I want to control the format of the cell dynamically, so I create a user control and try the following:
<ItemTemplate>
<myctl:ItemTemplateCtl runat=server />
</ItemTemplate>
Now I want to access the data record from the user control, and I find that if I use the following in the ItemTeplateCtl.ascx file:
<%# DataBinder.Eval(((DataListItem) Parent).DataItem, "StringValue") %>
Then it seems to work, and I get the data for that cell.
But wait! What I really want is for the cell format to be dynamically loaded from a file. So I create a new user control 'Details', and in my ItemTemplateClt user control, I create a placeholder as follows:
<asp:PlaceHolder ID="ph1" runat="server" ></asp:PlaceHolder>
Now in the code file for the ItemTemplateCtl control, I do the following:
protected void Page_Load(object sender, EventArgs e)
{
UserControl skin;
skin = (UserControl)TemplateControl.LoadControl("details.ascx");
ph1.Controls.Add(skin);
}
which adds the user control 'Details' the page. Now, in the details.ascx file, I want the data to appear, so I used the following line:
<%# DataBinder.Eval(((DataListItem) Parent.Parent).DataItem, "StringValue") %>
My thinking was that the parent of the 'Details' control is the 'ItemTemplateCtl', whose parent is the object which has my data.
Unfortunately this does not work. If I put a break point on the
<%# DataBinder.Eval(((DataListItem) Parent.Parent).DataItem, "StringValue") %>
line in the Details.ascx file, I find that when it breaks, that 'Parent' is null. However, if I also put a breakpoint in the Page_Load event handler for the Details control, I find that the Parent is no longer null, and if I look at Parent.Parent, it seems to be the object that has my data. However this is too late, since the 'DataBinder' line in the Details.ascx file has already been executed (and Parent was null at that time).
So, I have two questions.
1) Am I going about this the right way, or is there some much easier solution that I have overlooked (I am pretty new to both DotNetNuke and ASP.net)?
2) If there is no easier way, is there a way to make this work with what I am doing now?
Any help you can give, would be most appreciated. If this is not the correct forum for such a question, can someone direct me to where I might find the answer?
Thanks