asp.net – ASPNET WebForms服务器控件在Page_Load中为NULL
发布时间:2020-12-15 23:36:14 所属栏目:asp.Net 来源:网络整理
导读:我有一个非常奇怪的情况,我的Label控件在Page_Load中为NULL.甚至有可能吗 我有一个Web表单: %@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="MyNamespace.Templates.Pages.WebForm1" %%@ Register TagPrefix="cw" N
我有一个非常奇怪的情况,我的Label控件在Page_Load中为NULL.甚至有可能吗
我有一个Web表单: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="MyNamespace.Templates.Pages.WebForm1" %> <%@ Register TagPrefix="cw" Namespace="MyNamespace.Templates.Units" Assembly="MyNamespace" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div style="background-color:yellowgreen;"> FIRST </div> <div style="background-color: wheat;"> SECOND <cw:ArticleColumn runat="server" ID="ac" ArticleID="1899"/> </div> </form> </body> </html> 在网页表单的Page_Load中,我只做一个 if(!IsPostBack) DataBind(); 用户控件ArticleColumn如下所示: <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ArticleColumn.ascx.cs" Inherits="MyNamespace.Templates.Units.ArticleColumn" %> <h2><asp:Label runat="server" ID="myArticleHeading" Text="TOM" /></h2> <p><asp:Label runat="server" ID="myArticleIntro" Text="TOM" /></p> <asp:Label runat="server" ID="myArticleBody" Text="TOM" /> 后面的代码看起来像这样: public partial class ArticleColumn : UserControlBase<PageTypes.ArticleBase> { public int ArticleID { get; set; } protected void Page_Load(object sender,EventArgs e) { var page = DataFactory.Instance.GetPage(new PageReference(ArticleID)); myArticleHeading.Text = page.PageName; myArticleIntro.Text = page.Property["IntroText"] != null ? page.Property["IntroText"].ToWebString() : string.Empty; myArticleBody.Text = page.Property["MainBody"] != null ? page.Property["MainBody"].ToWebString() : string.Empty; DataBind(); } } 当我尝试设置myArticleHeading.Text时,它失败,因为控件为空.这怎么可能?控件的ASP.NET自动绑定必须以某种方式失败… 我努力了: 不用说,上述都没有. 编辑:添加堆栈跟踪: [NullReferenceException: Object reference not set to an instance of an object.] MyNamespace.Templates.Units.ArticleColumn.Page_Load(Object sender,EventArgs e) in C:DevProjectsCustomerWebMainSourceMyNamespaceTemplatesUnitsArticleColumn.ascx.cs:21 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp,Object o,Object t,EventArgs e) +14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender,EventArgs e) +35 System.Web.UI.Control.OnLoad(EventArgs e) +91 System.Web.UI.Control.LoadRecursive() +74 System.Web.UI.Control.LoadRecursive() +146 System.Web.UI.Control.LoadRecursive() +146 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint) +2207 解: <%@ Register TagPrefix="cw" TagName="ArticleColumn" Src="~/Templates/Units/ArticleColumn.ascx" %> 此错误声明使用ReSharper从html视图添加右键菜单: <%@ Register TagPrefix="cw" Namespace="MyNamespace.Templates.Units" Assembly="MyNamespace" %> 谢谢您的帮助! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 左加入日期,分组结果,在分组结果中显示所有
- asp.net – 对绑定到自定义通用对象列表的GridView进行排序
- 如何在asp.net中使用javascript为下拉列表框设置可见的true
- 如何为ASP.NET应用程序全局设置autocomplete = off?
- 获取所选的输入类型单选按钮ASP.NET
- asp.net-mvc – 在使用ORM解决方案的ASP.NET MVC中工作时,我
- asp.net-web-api – 如何在ConfigureServices方法中获取IOp
- asp.net – 项目模板和布局模板之间的区别
- ASP.Net 2中的上传文件在哪里?
- asp.net-core – 如何在aspnet核心应用程序中为静态文件使用
推荐文章
站长推荐
热点阅读