locked
Static Method for Repeater's ItemDataBound event - makes sense? RRS feed

  • Question

  • User-383681703 posted

    Good afternoon.

    I have two web forms each having a Repeater which is being populated with the same data, although data is displayed in a different way.

    In order not to replicate the code on both web forms, I created what I am calling a Helper class so as from BOTH web forms I can pass the datatable with the the data and the Repeater control.

    My concern before I keep on using this approach for the whole website is that the methods in the Helper class are both static and the ItemDataBound one is the one which is making me worry the most.

    Will I have bottleneck or performance problems by adopting this approach?

    The code in the Helper class is below:

    public static void ShowMonths(Repeater repMonths, DataTable dtMonths)
    {
    	repMonths.ItemDataBound += new RepeaterItemEventHandler(repMonths_ItemDataBound);
    	repMonths.DataSource = dtMonths;
    	repMonths.DataBind();
    }
    
    private static void repMonths_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
    
    	if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
    	{
    		((Literal)e.Item.FindControl("litMonthName")).Text = ((DataRowView)e.Item.DataItem)["monthName"]).ToString());
    	}
    
    }


    Sunday, May 30, 2010 12:18 PM

Answers

  • User626880745 posted

    If you bind the Literal declaratively, you probably wouldn't need the ItemDataBound (if that helps in taking that one off, and wouldn't need the dynamic wire-up for the ItemDataBound)

    <asp:Literal ID="Literal1" runat="server" Text='<%# Eval("ColumnName") %>' />

    all that would remain is the datasource to be bound which could be kept in the respective page(s).

    I guess I don't see the benefit with this approach.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 30, 2010 1:50 PM

All replies

  • User626880745 posted

    If the data is the same for both pages (repeaters in those pages) and doesn't change often you could persist the datasource (datatable etc) in the cache (see nine options to persist user state)

    If you don't want to replicate code you could use a usercontrol housing the Repeater and then use that usercontrol on multiple pages.


    Note that the ItemDataBound is going to be raised any and every time the data bound to the repeater changes or is changed.

    Sunday, May 30, 2010 12:47 PM
  • User-383681703 posted

    The data is already in the cache but mine is not a problem of data but of display.


    Also, I cannot host the Repeater in a user control as although the two Repeaters have the same web controls, the ItemTemplate is different, different CSS classes and so on.

    Sunday, May 30, 2010 12:55 PM
  • User626880745 posted

    If you bind the Literal declaratively, you probably wouldn't need the ItemDataBound (if that helps in taking that one off, and wouldn't need the dynamic wire-up for the ItemDataBound)

    <asp:Literal ID="Literal1" runat="server" Text='<%# Eval("ColumnName") %>' />

    all that would remain is the datasource to be bound which could be kept in the respective page(s).

    I guess I don't see the benefit with this approach.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 30, 2010 1:50 PM
  • User-1184423958 posted

    Alternatively,  use singleton pattern instead of static methods.

    http://stackoverflow.com/questions/46541/when-should-you-use-the-singleton-pattern-instead-of-a-static-class 

    Thursday, June 3, 2010 6:45 AM