我試圖在遷移舊的ASP.NET 網站項目成爲一個VS2010 Web應用程序項目。它似乎工作,但我發現用戶控件在.ascx文件具有所有自己的控件設置爲空。Web窗體用戶控件NULL字段上載
我懷疑它與我如何在需要控件的aspx文件中使用聲明有關,但我不確定。我創建了一個示例文件和頁面,並可以重現它。原始網站是動態編譯的,但是我想把它變成一個DLL,因爲沒有新的部署,任何東西都不會改變。
任何人都可以給出一個理由,爲什麼「TheTextBox」將在用戶控件的Page_Load上爲空,並可能的解決方案?
控制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自動生成的設計文件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;
}
}
web窗體包含控制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>
web窗體包含控制的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;
我確實有這種方法與有問題的用戶控件。在遷移應用程序的情況下,用戶是控件抱怨重複的類。這似乎是因爲控件被編譯到站點DLL中,並且在執行時再次編譯。只有這樣,我才能想到的是在一個單獨的課程中,我還沒有嘗試過。 – Jason
這篇文章救了我,我只是愚蠢地忘記了控制聲明:) – Anonymous