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

Using the Template Class in DataList

The following steps will take us through to use the above created MyTemplate class to create header, item and footer templates dynamically.

1.    Create a DataList object.

2.    Create an instance of our created dynamic template class by passing the ListItemType corresponding to the type of the item.

3.    Assign the instance to the datalist template property like:

Listing 3: DataList Templates

DataListObject.HeaderTemplate = new MyTemplate(ListItemType.Header);
DataListObject.ItemTemplate = new MyTemplate(ListItemType.Item);
DataListObject.FooterTemplate = new MyTemplate(ListItemType.Footer);

The below code contains code that uses the MyTemplate class to build the DataList dynamically. I have added a PlaceHolder in the WebForm which will hold the dynamically created DataList after the execution.

Listing 4: Using TemplateClass

protected void Page_Load(object sender, EventArgs e)
    {
for (int i = 0; i < dsArticles.Tables.Count; i++)
        {
            if (dsArticles.Tables[i].Rows.Count > 0)
            {
                DataList dlArt = ConstructDL(
                    dsArticles.Tables[i].Rows[0]["Category"].ToString(), 
                    dsArticles.Tables[i].Rows.Count);
                dlArt.DataSource = dsArticles.Tables[i];
                dlArt.DataBind();
                phArticles.Controls.Add(dlArt);
            }
        }  
    } 
 
public DataList ConstructDL(string CategoryName,int Count)
    {
        DataList dlArt = new DataList();
        MyTemplate headTemplate = new MyTemplate(ListItemType.Header);
        headTemplate.CategoryName = CategoryName;
        dlArt.HeaderTemplate = headTemplate;
        dlArt.Width = Unit.Percentage(100);
        dlArt.ItemTemplate = new MyTemplate(ListItemType.Item);
        MyTemplate footerTemplate = new MyTemplate(ListItemType.Footer);
        footerTemplate.NoOfArticles = Count.ToString();
        dlArt.FooterTemplate = footerTemplate;
        return dlArt;
    }

In the above code, phArticles is the PlaceHolder id. Thus, we have created the DataList dynamically and now we will move on to the implementation of dynamic GridView.


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 4 and 3 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 8:21:56 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search