Compilation error when dynamically setting property of user control inside repeater

All we need is an easy explanation of the problem, so here it is.

In a C# Webforms website (not web application), I have the following set up:

A page, which includes Step.ascx which includes an asp:Repeater.

The repeater in Step.ascx displays all sections (Section.ascx) within this step.

SectionInfoProvider.GetSections(1).ToList() returns a list of SectionInfo, which is a class several properties including DisplayName as string.

I’m getting a compilation error (The type or namespace name could not be found) when trying to dynamically set the SectionInfo property of the Section user control into the repeater in Step.ascx.cs. I’ve tried using the partial class name of the user control, and UserControl, but neither work.

I’m also trying to create a collection of SectionControls because I want to use that for something else on the page too.

What am I doing wrong here?

Step.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="~/Step.ascx.cs" Inherits="Custom_Step" %>
<%@ Register TagPrefix="uc" TagName="Section" Src="~/Section.ascx" %>

<asp:Repeater runat="server" ID="rptSections" OnItemDataBound="rptSections_OnItemDataBound" EnableViewState="True">
   <ItemTemplate>
      <uc:Section runat="server" ID="Section" EnableViewState="True"/>            
   </ItemTemplate>
</asp:Repeater>

Step.ascx.cs

public partial class Custom_Step : System.Web.UI.UserControl
{
    public IEnumerable<SectionInfo> Sections { get; set; }
    private IEnumerable<???> SectionControls { get; set; } <-- What should ??? be?

    protected override void OnLoad(EventArgs e)
    {
        Sections = SectionInfoProvider.GetSections(1).ToList();

        rptSections.DataSource = Sections;
        rptSections.DataBind();    
    }

    protected void rptSections_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        var info = (SectionInfo)e.Item.DataItem;
        var ctrl = (???)e.Item.FindControl("Section");  <-- What should ??? be?
        ctrl.SectionInfo = info;

        if (SectionControls == null) SectionControls = new List<???>();  <-- What should ??? be?
        ((List<???>)SectionControls).Add(ctrl); <-- What should ??? be?
    }    
}

Section.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="~/Section.ascx.cs" Inherits="Custom_Section " %>

<h3><asp:Literal runat="server" ID="litSectionHeading" /></h3>

Section.ascx.cs:

public partial class Custom_Section : System.Web.UI.UserControl
{
    public SectionInfo SectionInfo { get; set; }

    protected override void OnLoad(EventArgs e)
    {
        litSectionHeading.Text = SectionInfo.DisplayName;
    }
} 

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

I think you just need a typecast and an if.

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {`
...
var ctrl = e.Item.FindControl<Custom_Section>("Section");`
or
var ctrl = (Section)e.Item.FindControl("Section");
...
}

Is that something you already tried?

Creating the dynamic controls can be done but that’s a separate question.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply