I am trying to migrate an old ASP.NET Website to become a Web Application project in VS2010. This basically works, however I found that the User Controls in the .ascx files have all their own controls set to null.
I suspect this has something to do with how I use declarations in aspx files that need controls, but I'm not sure. I created a sample file and page and can play it. The source site has been dynamically compiled, however, I am trying to turn it into a single DLL, since nothing will change without a new deployment.
Can someone explain the reason the "TheTextBox" will be empty on the user_Load page of the user control and a possible solution?
ASCX control 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" />
Managing 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) {
Managing the AutoGenerated ASCX WebUserControl1.ascx.designer.cs Designer File
namespace MyNamespace.Ascx { public partial class WebUserControl1 {
WebForm containing the WebForm1.aspx control
<%@ 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 CodeFile control 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) { } } }
Change As suggested, replacing the WebForm directive with
<%@ Register Src="~/ascx/WebUserControl1.ascx" TagName="WebUserControl1" TagPrefix="prefix" %>
leads to a beautiful yellow screen of death:
CS0260: Partial modifier is missing when declaring type "PSVOnline.Ascx.WebUserControl1"; there is another partial declaration of this type
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;