Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my Repeater null in code behind?

Tags:

c#

asp.net

I'm just starting a new project and I am getting some really weird stuff happening.

ASP.NET 3.5, VS2008.

I've tried rebuild, close VS, delete everything and get from svn again but I cannot understand why the repeater in the following is null on page_load.

I know this is going to be a headslapping moment. Help me out?

Markup:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="GalleryControl.ascx.cs" Inherits="Site.UserControls.GalleryControl" %>
<asp:Repeater ID="rptGalleries" runat="server">
    <HeaderTemplate><ul></HeaderTemplate>
    <ItemTemplate>
       <li>wqe</li>
    </ItemTemplate>
    <FooterTemplate></ul></FooterTemplate>
</asp:Repeater>

Code behind

public partial class GalleryControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        rptGalleries.DataSource = new[] {1, 2, 3, 4, 5};
        rptGalleries.DataBind();
    }
}

Designer:

  public partial class GalleryControl {

    /// <summary>
    /// rptGalleries control.
    /// </summary>
    /// <remarks>
    /// Auto-generated field.
    /// To modify move field declaration from designer file to code-behind file.
    /// </remarks>
    protected global::System.Web.UI.WebControls.Repeater rptGalleries;
}

Why is my repeater null? What the F is going on?

The referencing page has this:

<ux:GalleryControl runat="server" ID="uxGalleryControl"/>

The web.config has this (I've never had to do this before but my masterpage was complaining about not finding another user control).

<add tagPrefix="ux" namespace="Site.UserControls" assembly="Site" />
like image 292
Rob Stevenson-Leggett Avatar asked Jun 10 '10 00:06

Rob Stevenson-Leggett


People also ask

How do I know if my Repeater is empty?

Like GridView, Repeater control does not have any EmptyDataTemplate and hence we need to make use of FooterTemplate and programmatically show (display) empty data message from code behind when no records are present. The HTML Markup consists of an ASP.Net Repeater control and a Button.

What does a Repeater do in asp net?

The Repeater control is used to display a repeated list of items that are bound to the control. The Repeater control may be bound to a database table, an XML file, or another list of items.

CAN Repeater filter data?

Yes, but it's not Category Wise. It's filtering Data. In my case all Particulars fields have type.

What is Repeater in VB net?

The Repeater control as the name suggests will repeat its content and hence it can be used to display data using any HTML element. In this article, the Repeater control will be populated from database and the data will be displayed as HTML table on the page.


1 Answers

After hours of head banging I have finally figured this out.

I was referencing the User controls in the web config as stated (I also tried the Register method with the Assembly). I think there's a weirdness with this method when the controls are in the same assembly. So referencing them like this:

<%@ Register Src="~/UserControls/GalleryControl.ascx" TagPrefix="ux" TagName="GalleryControl" %>

Worked immediately.

I hope anyone else with the same problem finds this useful.

like image 57
Rob Stevenson-Leggett Avatar answered Oct 15 '22 16:10

Rob Stevenson-Leggett