开发者

Web Forms User Control Null Fields on Load

开发者 https://www.devze.com 2023-04-11 22:07 出处:网络
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

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;


Typically when I register a user control, instead of what you have on WebForm1.aspx I register them like so:

<%@ Register Src="~/WebUserControl1.ascx" TagName="control" TagPrefix="uc" %>

I don't know if that's the problem you're having, but that doesn't look like the typical way to register a user control.


I've found the solution..

Using the following approach works perfectly..

http://www.codeproject.com/KB/aspnet/ASP2UserControlLibrary.aspx

0

精彩评论

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