multiple itemtemplates datalist

Discussion in 'ASP .Net Web Controls' started by krallabandi@gmail.com, Jan 6, 2006.

  1. Guest

    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.
    , Jan 6, 2006
    #1
    1. Advertising

  2. 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();
    }
    }
    }
    Wouter van Vugt, Jan 10, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. aaapaul
    Replies:
    3
    Views:
    426
    Martin Marinov
    Jun 21, 2004
  2. Luis Esteban Valencia

    Datagrid with textboxes on itemtemplates

    Luis Esteban Valencia, Jan 26, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    338
    Luis Esteban Valencia
    Jan 26, 2005
  3. Replies:
    2
    Views:
    2,566
  4. Mr Not So Know It All

    Dynamic Repeater and ItemTemplates

    Mr Not So Know It All, Jan 20, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    882
    Mr Not So Know It All
    Jan 24, 2007
  5. JPabich
    Replies:
    0
    Views:
    755
    JPabich
    Jun 4, 2007
Loading...

Share This Page