开发者

Why is my Repeater null in code behind?

开发者 https://www.devze.com 2023-01-03 10:21 出处:网络
I\'m just starting a new project and I am getting some really weird stuff happening. ASP.NET 3.5, VS2008.

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开发者_Python百科></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" />


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.


I had a similar issue, only to realise that the repeater in question was in the (massive) header template of another repeater. Rearranging the header template in such a way that said repeater was outside, solved the issue... Weird that there were no compiler errors though

0

精彩评论

暂无评论...
验证码 换一张
取 消