Repeater ITemplate all in one table

Discussion in 'ASP .Net Web Controls' started by Matheson, Oct 29, 2003.

  1. Matheson

    Matheson Guest

    I'm currently using classes implementing the ITemplate interface to
    format the display of data that is bound to databound objects. I
    started out using the DataList, but soon found that the datalist
    performs some initial formatting for you. Specifically, it
    automatically puts any bound data into a table row and table cell for
    you (<tr><td>[my bound data column1]</td></tr>).

    That's great, but what if you want to add another column and have it
    align with the rest of the data? (<tr><td>[my bound data
    column1]</td><td>[my bound data column2]</td></tr>)

    So I'm trying to use the Repeater instead. It has no automatic
    formatting. The end result I'm looking for is to be able to
    dynamically decide which ITemplate to load for formatting the data and
    then have all that data presented in one html table. I've seen
    examples that accomplish this by using the ITemplate to manipulate a
    string of HTML, but this seems limited. How would you then be able to
    bind events to controls you wanted to add to each row, like
    LinkButtons and such?

    The ITemplate I am currently working with is listed below. If anybody
    sees anything silly or can point me in a different direction, I would
    really appreciate it. Thanks.

    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace sane2
    {
    public class IssuesTemplate : ITemplate
    {
    ListItemType templateType;

    int count=0;
    Table thisTable = new Table();

    public void InstantiateIn(System.Web.UI.Control container){
    thisTable.BorderWidth = 4;
    thisTable.GridLines = GridLines.Both;
    TableRow TR;
    TableCell TD;

    Label myLabel = new Label();

    switch(templateType){
    case ListItemType.Header:
    TR = new TableRow();
    TD = new TableCell();
    TD.Text = "Header Text";
    TR.Cells.Add(TD);
    thisTable.Rows.Add(TR);
    break;

    case ListItemType.Item:
    myLabel = new Label();
    myLabel.Text = count.ToString();
    myLabel.DataBinding += new EventHandler(myLabel_DataBinding);

    TR = new TableRow();
    TD = new TableCell();
    TD.Controls.Add(myLabel);
    TR.Cells.Add(TD);
    thisTable.Rows.Add(TR);
    break;

    case ListItemType.Footer:
    Label myLabel2 = new Label();
    myLabel2.Text = "Footer Text";

    TR = new TableRow();
    TD = new TableCell();
    TD.Controls.Add(myLabel2);
    TR.Cells.Add(TD);
    thisTable.Rows.Add(TR);

    break;
    }
    container.Controls.Add(thisTable);
    count++;
    //container.Controls.Add(myLabel);
    }

    private void myLabel_DataBinding(object sender, System.EventArgs e)
    {
    Label myControl = (Label) sender;
    RepeaterItem container = (RepeaterItem) myControl.NamingContainer;


    myControl.Text = "<br>[" + DataBinder.Eval(container.DataItem,
    "IssueID") + "]";
    }

    public IssuesTemplate(ListItemType type)
    {
    templateType = type;
    }
    }
    }
    Matheson, Oct 29, 2003
    #1
    1. Advertising

  2. Matheson

    MSFT Guest

    Hi Matheson,

    How did you use the class IssuesTemplate in a DataList/Repeater control,
    set it to ItemTemplate with code? To bind events to controls, I have seen
    you bound databinding for the label control, is this not what you what?

    Luke
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
    MSFT, Oct 30, 2003
    #2
    1. Advertising

  3. Matheson

    Matheson Guest

    This is in the pageLoad event (myDV is a populated Dataiew):

    Repeater myR = new Repeater();
    myR.DataSource = myDV;
    myR.HeaderTemplate = new IssuesTemplate(ListItemType.Header);
    myR.ItemTemplate = new IssuesTemplate(ListItemType.Item);
    myR.FooterTemplate = new IssuesTemplate(ListItemType.Footer);
    myR.DataBind();
    Page.Controls.Add(myR);



    (MSFT) wrote in message news:<>...
    > Hi Matheson,
    >
    > How did you use the class IssuesTemplate in a DataList/Repeater control,
    > set it to ItemTemplate with code? To bind events to controls, I have seen
    > you bound databinding for the label control, is this not what you what?
    >
    > Luke
    > Microsoft Online Support
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    Matheson, Oct 31, 2003
    #3
  4. Matheson

    MSFT Guest

    Hi Matheson,

    To bind events to controls in the Repeater, I think you may add some code
    in its ItemDataBound event, In this method, you can get each control in the
    particualr row by:

    e.Item.FindControl("ControlName")

    And then use Addhandler to bind the control's event to any method you like.


    Luke
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
    MSFT, Oct 31, 2003
    #4
    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. cosine... zero
    Replies:
    0
    Views:
    642
    cosine... zero
    Jun 9, 2004
  2. cosine... zero
    Replies:
    0
    Views:
    765
    cosine... zero
    Jun 11, 2004
  3. Luhar Powell via .NET 247
    Replies:
    0
    Views:
    2,439
    Luhar Powell via .NET 247
    Apr 1, 2005
  4. Luhar Powell via .NET 247

    Newbie:Access custom Itemplate(not datagrid/repeater/datalist) control values on postback

    Luhar Powell via .NET 247, Apr 1, 2005, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    210
    Luhar Powell via .NET 247
    Apr 1, 2005
  5. Luhar Powell via .NET 247
    Replies:
    0
    Views:
    159
    Luhar Powell via .NET 247
    Apr 1, 2005
Loading...

Share This Page