加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – Web窗体用户控件加载时的空字段

发布时间:2020-12-16 09:49:47 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试迁移旧的ASP.NET网站项目,以成为VS2010中的Web应用程序项目.它似乎工作,但我发现.ascx文件中的用户控件将所有自己的控件设置为null. 我怀疑它与我如何在需要控件的aspx文件中使用声明有关,但我不确定.我创建了一个示例文件和页面,可以重现它.原始
我正在尝试迁移旧的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包含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包含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)
        {

        }
    }
}

编辑:按照建议,用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;

解决方法

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

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

我不知道这是你遇到的问题,但这看起来不像是注册用户控件的典型方式.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读