I am attempting to migrate an older ASP.NET Web Site project over to become a Web Application project in VS2010. It mostly appears to work, however I am finding that User Controls in .ascx files have all their own controls set as null.
I suspect it has something to do with how I am using declarations in the aspx files that need the controls, but I am uncertain. I created a sample file and page and can reproduce it. The original site was dynamically compiled, however I am trying turn it into a single DLL since nothing will ever change without a new deployment.
Can anyone give a reason why the "TheTextBox" would be null on Page_Load of the user control, and possible solution?
Control ASCX File WebUserControl1.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl1.ascx.cs" Inherits="MyNamespace.Ascx.WebUserControl1" ClassName="MyNamespace.Ascx.WebUserControl1" %>
<asp:TextBox runat="server" ID="TheTextBox" MaxLength="128" />
Control ASCX CodeFile WebUserControl1.ascx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MyNamespace.Ascx
{
public partial class WebUserControl1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
// TheTextBox is null at this point. Why?
}
}
}
Control ASCX AutoGenerated Designer File WebUserControl1.ascx.designer.cs
namespace MyNamespace.Ascx {
public partial class WebUserControl1 {
/// <summary>
/// TheTextBox 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.TextBox TheTextBox;
}
}
WebForm Containing the Control WebForm1.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" Inherits="MyNamespace.WebForm1" Codebehind="WebForm1.aspx.cs" %>
<%@ Register NameSpace="MyNamespace.Ascx" Assembly="MyAssembly" TagPrefix="prefix" %>
<asp:Content ID="Content1" ContentPlaceHolderID="PageHead" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<prefix:WebUserControl1 runat="server" ID="IncludedControl" />
</asp:Content>
WebForm Containing the Control CodeFile WebForm1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MyNamespace
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
Edit: As suggested, replacing the WebForm directive with
<%@ Register Src="~/ascx/WebUserControl1.ascx" TagName="WebUserControl1" TagPrefix="prefix" %>
results in a lovely Yellow Screen of Death:
CS0260: Missing partial modifier on declaration of type 'PSVOnline.Ascx.WebUserControl1'; another partial declaration of this type exists
Line 129:
Line 130: [System.Runtime.CompilerServices.CompilerGlobalScopeAttribute()]
Line 131: public class WebUserControl1 : global::MyNamespace.Ascx.WebUserControl1 {
Line 132:
Line 133: private static bool @__initialized;