Dynamic GridView and DataList in ASP.NET 2.0
page 4 of 10
by Satheesh Babu
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 36382/ 791

Implementing Dynamic DataList

The DataList will have a Header template with Category Name displayed. For example, if it is displaying ASP.NET article, the category name will be ASP.NET. The articles will be displayed in the item template. For every article in the item template we will display the title of the article as a hyperlink that links to the original URL of the article with a description and author name. The footer will show the number of articles present in this category.

Listing 2: TemplateClass for DataList

public class MyTemplate : ITemplate
{
    ListItemType _itemType;
    private string _no;
    private string _categoryName;
    public string NoOfArticles
    {
        get
        {
            return _no;
        }
        set
        {
            _no = value;
        }
 
    }
    public string CategoryName
    {
        get
        {
            return _categoryName;
        }
        set
        {
            _categoryName = value;
        }
    }
    public MyTemplate(ListItemType Type)
    {
        _itemType = Type;
    }
    public void InstantiateIn(System.Web.UI.Control container)
    {
        Literal lc = new Literal();
        switch (_itemType)
        {
            case ListItemType.Header:
                lc.Text = "<div id=\"nifty\" class=\"PostCategory\">   " 
                            + CategoryName + "</div>";
                break;
            case ListItemType.Item:
                lc.DataBinding += new EventHandler(TemplateControl_DataBinding);
                break;
            case ListItemType.Footer:
                lc.Text = "<div style=\"text-align:right\">" + NoOfArticles 
                           + " Article(s) present in this category" + "</div>";
                break;
        }
        container.Controls.Add(lc);
    }
private void TemplateControl_DataBinding(object sender,  System.EventArgs e)
    {
        Literal lc;
        lc = (Literal)sender;
        DataListItem container = (DataListItem)lc.NamingContainer;
   lc.Text += "<div class=\"Post\"><div class=\"PostTitle\"><A     href=" + 
      DataBinder.Eval(container.DataItem, "URL"+ ">" + 
      DataBinder.Eval(container.DataItem, "Title"+ 
      "</a></div><div class=\"PostSubtitle\">" + 
      DataBinder.Eval(container.DataItem, "Description"+ 
      "</div> <div class=\"PostInfo\">Posted on " + 
      DataBinder.Eval(container.DataItem, "CreatedOn""{0:d} @ {0:t}"+ " By " + 
      DataBinder.Eval(container.DataItem, "CreatedBy"+ " in " + 
      DataBinder.Eval(container.DataItem, "Category"+ "</div> ";
    }
}

In the above code I have used 2 public properties, CategoryName and NoOfArticle, for displaying category name in Header Template and for displaying a number of articles in Footer Template. We can also specify this as constructor argument instead of giving it as public properties. I have defined TemplateControl_DataBinding event to bind the data that is in resultset.


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 7 and 2 and type the answer here:

User Comments

Title: JSON LINQ   
Name: willian brasil
Date: 10/3/2008 1:18:03 PM
Comment:
Very Good!
Title: Data List   
Name: Deleep Kumar Pavana
Date: 9/11/2008 5:03:07 AM
Comment:
Hi satheesh,
Its good but repeat colomns property is not working properly on that,if i use table tag to display data in table format..
Title: Good Work ....   
Name: Mudassar C
Date: 7/17/2008 1:09:10 AM
Comment:
Hi Sateesh,
Its a really good article to read..
Good piece of Code..

Mudassar Chandle
Title: Data List And GridView   
Name: Santosh Kumar
Date: 6/24/2008 3:36:54 AM
Comment:
Hi Satheesh,
I read this article , good work done.
Santosh Kumar
http://www.operativesystems.com
Title: VB Please   
Name: Matt
Date: 6/3/2008 1:11:24 AM
Comment:
Any chance I can get this in VB?
Please
THanks
Matt
Title: problem with binding   
Name: Kannan
Date: 5/15/2008 2:51:40 AM
Comment:
All item template values displayed at once.no reapeated datalist display
Title: muhajiran bugok   
Name: muhajiran bugok
Date: 5/9/2008 6:43:36 AM
Comment:
this is very helpful..
-putang ina nyo lahat
Title: sivaram   
Name: sivaram
Date: 2/4/2008 1:47:59 AM
Comment:
Thanks.

hi i read your article.it is well and good article.

Product Spotlight
Product Spotlight 
Learn More
.NET Tools
asp.net shopping cart
asp.net chart control






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2008 ASPAlliance.com  |  Page Processed at 12/3/2008 9:15:51 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search