Products

Solutions

Resources

Partners

Community

Blog

About

QA

Ideas Test

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeArchived Discus...Archived Discus...Developing Under Previous Versions of .NETDeveloping Under Previous Versions of .NETASP.Net 2.0ASP.Net 2.0Help using the DataList controlHelp using the DataList control
Previous
 
Next
New Post
10/7/2007 11:33 AM
 
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
 
 
New Post
10/7/2007 10:04 PM
 

Have you considered handling the ItemDataBound event of the DataList? When one needs to make formatting changes, etc. to a particular cell based on its data, this is the way to go.  Try a Google search on ItemDataBound and you'll find many examples/tutorials.


Bill, WESNet Designs
Team Lead - DotNetNuke Gallery Module Project (Not Actively Being Developed)
Extensions Forge Projects . . .
Current: UserExport, ContentDeJour, ePrayer, DNN NewsTicker, By Invitation
Coming Soon: FRBO-For Rent By Owner
 
New Post
10/7/2007 11:42 PM
 

I have played with the ItemDataBound event a little, and it seemed like I had a similar problem.  The real question is how do I propagate the data from the table cell  into a user control which I load when the cell is created?

I will take another look at the ItemDataBound though.  I might have missed something there.

Thanks for the reply.

John Gaby

 
New Post
10/8/2007 12:19 PM
 
Ok, I am now using the ItemDataBound event for the DataList control, and it simplifies things a bit, but I am still having trouble getting it to work.
 
Now the DataList declaration in the .ascx file looks like:
 
      <asp:DataList id="ItemsList"
           onItemDataBound="DataList_ItemDataBound"
           RepeatDirection="Horizontal"
           RepeatLayout="Table"
           RepeatColumns="3"
           runat="server">
 
         <HeaderStyle BackColor="#aaaadd">
         </HeaderStyle>
 
         <AlternatingItemStyle BackColor="Gainsboro">
         </AlternatingItemStyle>
 
         <HeaderTemplate>
            List of items
         </HeaderTemplate>
              
         <ItemTemplate>
            <asp:PlaceHolder runat="server" ID="ItemTemplatePlaceholder" />
         </ItemTemplate>
 
      </asp:DataList>
 
and the function to handle the ItemDataBound event looks like:
 
       void DataList_ItemDataBound(object sender, DataListItemEventArgs e)
       {
           switch (e.Item.ItemType)
           {
               case ListItemType.Item:
                   PlaceHolder pl;
                   UserControl skin;
 
                   skin = (UserControl)TemplateControl.LoadControl("details.ascx");
                  
                   pl = (PlaceHolder)e.Item.FindControl("ItemTemplatePlaceholder");
                   pl.Controls.Add(skin);
                   break;
           }
       }

 

Finally, in the details.ascx file I have:

 
<%@ Control Language="C#" ClassName="DetailsCS" AutoEventWireup="true" CodeFile="Details.ascx.cs" Inherits="Details" %>
 
    <%# DataBinder.Eval(((DataListItem) Parent).DataItem, "StringValue") %>
--------
 
Once again, however, when I put a breakpoint on the '<%# DataBinder...>' line, I find that 'Parent' is null.  If I also put a breakpoint on the Page_Load or Page_Init lines of the details.ascx.cs file, I find that Parent is no longer null, but these functions are called after the '<%# DataBinder...>' line has been processed.
 
How do I get my data into my user control, so that I can display it?
 
Once again, thanks for the help.
 
John Gaby
 
 

 
New Post
10/8/2007 6:18 PM
 

Although I don't quite see the need to create  and load the details.ascx as an intermediary object, I guess this is one way to dynamically alter the display of a data item based on a template.  I also assume that your details.ascx user control exposes a DataSource property and Databind method. If so, you might want to try the following:

1. Handle the ItemCreated event of the DataList during which you would use LoadControl to load details.ascx and attach it to the "ItemTemplatePlaceholder" - like you are now doing in the ItemDataBound event

2. Handle the ItemDataBound event of the DataList during which you would do the actual databinding. Note that the data item that will be bound to the DataList's ItemTemplate is available during this event as the object e.Item.DataItem. You can then cast this object to whatever class represents the data of each item - for example, to a custom business object. So you might do something like this (may not be exactly correct as C# is not my primary language):

if (e.Item.ItemType == ListItemType.Item) {
       Details dtl = (Details) e.Item.FindControl("MyDetailsControlID");
       dtl.DataSource = (MyItemDataType) e.Item.DataItem;
       dtl.Databind();
}

An even simpler approach (and the on that many core modules using a content template employ) would be to handle the ItemDataBound event to fetch a html coded content string containing tags such as [Name], [Address], etc. for each field of data, replace the tags with the corresponding data (obtained from e.Item.DataItem), and inject the processed string into a label or placeholder in the ItemTemplate.


Bill, WESNet Designs
Team Lead - DotNetNuke Gallery Module Project (Not Actively Being Developed)
Extensions Forge Projects . . .
Current: UserExport, ContentDeJour, ePrayer, DNN NewsTicker, By Invitation
Coming Soon: FRBO-For Rent By Owner
 
Previous
 
Next
HomeHomeArchived Discus...Archived Discus...Developing Under Previous Versions of .NETDeveloping Under Previous Versions of .NETASP.Net 2.0ASP.Net 2.0Help using the DataList controlHelp using the DataList control


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out