Web窗体用户控制加载时的空字段

时间:2011-10-09 01:15:03

标签: asp.net user-controls webforms

我正在尝试迁移较旧的ASP.NET 网站项目,以成为VS2010中的 Web应用程序项目。它似乎工作,但我发现 .ascx 文件中的用户控件将所有自己的控件设置为null。

我怀疑它与我如何在需要控件的aspx文件中使用声明有关,但我不确定。我创建了一个示例文件和页面,可以重现它。原始站点是动态编译的,但我尝试将其转换为单个DLL,因为没有新的部署,任何事情都不会改变。

任何人都可以说明为什么“TheTextBox”在用户控件的Page_Load上为null,以及可能的解决方案?

控制ASCX文件 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" />  

控制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?
        }
    }
} 

控制ASCX AutoGenerated Designer文件 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 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>

包含控制CodeFile的WebForm 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)
        {

        }
    }
}

编辑:按照建议,用

替换WebForm指令
<%@ Register Src="~/ascx/WebUserControl1.ascx" TagName="WebUserControl1" TagPrefix="prefix" %>

导致一个可爱的黄色死亡屏幕:

CS0260:声明类型'PSVOnline.Ascx.WebUserControl1'时缺少部分修饰符;存在此类型的另一部分声明

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;

2 个答案:

答案 0 :(得分:3)

通常当我注册用户控件时,而不是你在WebForm1.aspx上注册的用户控件,我会这样注册它们:

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

我不知道你的问题是否存在,但这看起来不像是注册用户控件的典型方式。

答案 1 :(得分:0)

我找到了解决方案..

使用以下方法可以很好地工作..

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

相关问题