Wednesday, August 3, 2011

Creating Dynamic DataList control with ITemplate in C#

 

 

A bit of information on Templates available

Templates

The contents of the DataList control can be manipulated by using templates. The following table lists the supported templates.

Template Name

Description

AlternatingItemTemplate

If defined, provides the content and layout for alternating items in the DataList. If not defined, ItemTemplate is used.

EditItemTemplate

If defined, provides the content and layout for the item currently being edited in the DataList. If not defined, ItemTemplate is used.

FooterTemplate

If defined, provides the content and layout for the footer section of the DataList. If not defined, a footer section will not be displayed.

HeaderTemplate

If defined, provides the content and layout for the header section of the DataList. If not defined, a header section will not be displayed.

ItemTemplate

Required template that provides the content and layout for items in the DataList.

SelectedItemTemplate

If defined, provides the content and layout for the currently selected item in the DataList. If not defined, ItemTemplate is used.

SeparatorTemplate

If defined, provides the content and layout for the separator between items in the DataList. If not defined, a separator will not be displayed.

At the very minimum, the ItemTemplate needs to be defined to display the items in the DataList control. Additional templates can be used to provide a custom look to the DataList control.

For more read:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datalist.aspx

 

The use of custom template class to show desired data from inside the .cs class in C# could be easily achieved by having a custom class which will inherit from the ITemplate interface and manipulating the InstantiateIn Method

 

Let us get to the code for better understanding. We will first see the custom template class as MyTemplate. It will create the link items.

 

public class MyTemplate : ITemplate

{

    ListItemType ItemType;

    public MyTemplate(ListItemType _ItemType)

    {

        ItemType = _ItemType;

    }

    //This is the implentation fo the ITemplate interface

    #region ITemplate Members

    public void InstantiateIn(Control container)

    {

        Literal lc = new Literal();

        LinkButton lbItem = new LinkButton();

        //Determining which type of ListItem has come header or item etc.

        switch (ItemType)

        {

            case ListItemType.Header:

                lc.Text = "<div id=\"nifty\" class=\"PostCategory\">Header</div>";

                break;

            case ListItemType.Item:

                lc.DataBinding += new EventHandler(lc_DataBinding);

                break;

            case ListItemType.SelectedItem:

                lc.Text = "<div style='background-color:Red'><A href=# >Test Link</a></div> ";

                break;

            case ListItemType.Footer:

                lc.Text = "<div style=\"text-align:right\">Footer</div>";

                break;

        }

        //Adding to the container - here container is the DataList

        container.Controls.Add(lc);

    }

    private void lc_DataBinding(object sender, System.EventArgs e)

    {

        Literal lc = (Literal)sender;

        DataListItem container = (DataListItem)lc.NamingContainer;

        lc.Text += "<div style='background-color:#FFFFCC'><A href=" + DataBinder.Eval(container.DataItem, "Links") + ">" + DataBinder.Eval(container.DataItem, "Links") + " </a></div> ";

    }

    #endregion

}

 

Now to let us see how to use this class. Need to define which template and call accordingly.

 

#region Controls  + Variables

    Panel pnlControlPlaceHolder;

    #endregion

 

    protected void Page_Load(object sender, EventArgs e)

    {

        pnlControlPlaceHolder = new Panel();

        DataList myDataListL = new DataList();

        ////Populate Data

        DataTable dt = new DataTable();

        dt.Columns.Add("Links");

        DataRow dr;

        for (int i = 1; i < 10; i++)

        {

            dr = dt.NewRow();

            dr["Links"] = "A" + i;

            dt.Rows.Add(dr);

            dt.AcceptChanges();

        }

        //Data List propertise + Data bind

        myDataListL.RepeatDirection = RepeatDirection.Horizontal;

        myDataListL.RepeatColumns = 5;

        if (dt.Rows.Count > 0)

        {

            myDataListL.HeaderTemplate = new MyTemplate(ListItemType.Header);

            myDataListL.FooterTemplate = new MyTemplate(ListItemType.Footer);

            myDataListL.ItemTemplate = new MyTemplate(ListItemType.Item);

            myDataListL.SelectedItemTemplate = new MyTemplate(ListItemType.SelectedItem);

            myDataListL.Width = Unit.Percentage(100);

            myDataListL.GridLines = GridLines.Both;

            myDataListL.DataSource = dt;

            myDataListL.DataBind();

            pnlControlPlaceHolder.Controls.Add(myDataListL);

        }

        this.Controls.Add(pnlControlPlaceHolder);

    }

 

I have used this in an asp.net page to populate the controls. You just need to add this code in the code behind class of the aspx page.


If you have a more elegant solution – please post a comment… I’ll be happy to hear. Thanks for reading.

...HaPpY CoDiNg

Partha (Aurum)

 

No comments:

Post a Comment