Home All Groups Group Topic Archive Search About
Author
6 Jan 2006 9:27 PM
krallabandi
Hi, Can a datalist supports multiple itemtemplates? I want to have 2
itemtemplates in a single datalist. In 1st template I will display the
data from one dataset and in another I will display data from 2nd
dataset.

Is it possible? any knowledge base article for this?

Thanks.

Author
10 Jan 2006 4:43 PM
Wouter van Vugt
You could set the template programmatically to do this. The DataList
only supports one ItemTemplate by default. Here is an example which
also uses databinding usually found in a DataList template markup. The
AnnouncementsControl is some random control I've built a while ago.

Using two of these classes, you could achieve your goal.
With this sort of code, you can call something like

myDataList.ItemTemplate = new
AnnouncementsTemplate(myAnnouncementsControl)

Grtz,

Wouter van Vugt
Trainer - Info Support
http://blogs.infosupport.com/wouterv


        class AnnouncementsTemplate : ITemplate
        {
            AnnouncementsControl _control = null;

            public AnnouncementsTemplate(AnnouncementsControl control)
            {
                _control = control;
            }

            public void InstantiateIn(Control container)
            {
                if (_control.Settings.ShowMessageDate)
                {
                    LinkButton selectButton = new LinkButton();
                    selectButton.CommandName = "Select";
                    selectButton.DataBinding += new
EventHandler(selectButton_DataBinding);
                    container.Controls.Add(selectButton);
                    container.Controls.Add(new LiteralControl("  "));
                }
                Label titleLabel = new Label();
                titleLabel.DataBinding += new EventHandler(titleLabel_DataBinding);
                container.Controls.Add(titleLabel);
                container.Controls.Add(new LiteralControl("<BR/>"));

                Label bodyLabel = new Label();
                bodyLabel.DataBinding += new EventHandler(bodyLabel_DataBinding);
                container.Controls.Add(bodyLabel);
                container.Controls.Add(new LiteralControl("<BR/>"));
            }

            void bodyLabel_DataBinding(object sender, EventArgs e)
            {
                Label label = (Label)sender;
                label.Text =
(string)DataBinder.Eval(((DataListItem)label.NamingContainer).DataItem,
                    "Text");
            }

            void titleLabel_DataBinding(object sender, EventArgs e)
            {
                Label label = (Label)sender;
                label.Text =
(string)DataBinder.Eval(((DataListItem)label.NamingContainer).DataItem,
                    "Title");
            }

            void selectButton_DataBinding(object sender, EventArgs e)
            {
                LinkButton button = (LinkButton)sender;
                DateTime date =
(DateTime)DataBinder.Eval(((DataListItem)button.NamingContainer).DataItem,
"Date");
                try
                {
                    button.Text = date.ToString(_control.Settings.DateFormat);
                }
                catch (FormatException)
                {
                    button.Text = date.ToShortDateString();
                }
            }
        }