multiple itemtemplates datalist

K

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.
 
W

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();
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top