为什么这个字符串保持为空?

Why is this String remaining null?

我的 WebPart 上的控件没有被创建,因为条件:

if (this.dpsvisWebPart != null && this.dpsvisWebPart.CustomTitleProp != null)

...由于 CustomTitleProp 为空而失败。然而,它是在 WebPart 的构造函数中声明的,不应为 null。

代码如下:

namespace DirectPaymentSectionsWebPart.DPSVisualWebPart
{
    [ToolboxItemAttribute(false)]
    public class DPSVisualWebPart : WebPart
    {
        // Text
        [WebBrowsable(true),
        Category("Financial Affairs Forms"),
        Personalizable(PersonalizationScope.Shared),
        WebDisplayName("WebPart Title Text")]        
        public string CustomTitleProp { get; set; }

        // Checkboxes
        [WebBrowsable(true),
        Category("Financial Affairs Forms"),
        Personalizable(PersonalizationScope.Shared),
        WebDisplayName("Generate Section1")]
        public bool CheckboxGenSection1 { get; set; }

            . . .

// Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/DirectPaymentSectionsWebPart/DPSVisualWebPart/DPSVisualWebPartUserControl.ascx";
        . . .

        protected override void CreateChildControls()
        {
            //base.CreateChildControls(); // added this 5/28/2015 <= commented out, because it is being called over and over again
            DPSVisualWebPartUserControl control = Page.LoadControl(_ascxPath) as DPSVisualWebPartUserControl;
            //DPSVisualWebPartUserControl control = Page.LoadControl(siteUrl) as DPSVisualWebPartUserControl;
            if (control != null)
            {
                control.dpsvisWebPart = this;
            }
            Controls.Add(control);
        }
    } // class DPSVisualWebPart

那么,为什么 CustomTitleProp 在前面提到的时刻可能为空?这里的代码有更多的上下文:

public partial class DPSVisualWebPartUserControl : UserControl
{
    public DPSVisualWebPart dpsvisWebPart { get; set; }
    System.Web.UI.ScriptManager scriptMgr;

    . . .

    protected void Page_Load(object sender, EventArgs e)
    {
        base.OnPreRender(e);

        if (System.Web.UI.ScriptManager.GetCurrent(this.Page) == null)
        {
            scriptMgr = new System.Web.UI.ScriptManager();
            scriptMgr.EnablePartialRendering = true;
            this.Controls.AddAt(0, scriptMgr);
        }

        if (this.dpsvisWebPart != null && this.dpsvisWebPart.CustomTitleProp != null)
        {
            lbl_Title.Text = String.Format("<h1>{0}</h1>", this.dpsvisWebPart.CustomTitleProp.ToString());
        . . .

更新

在 "Adventures in Folder Diffing" 的标题下是这样的:

遗留项目包含一个名为 FileListAbsolute.txt 的文件,其内容为:

C:\Projects\DirectPaymentSectionsWebPart\DirectPaymentSectionsWebPart\bin\Debug\DirectPaymentSectionsWebPart.dll
C:\Projects\DirectPaymentSectionsWebPart\DirectPaymentSectionsWebPart\bin\Debug\DirectPaymentSectionsWebPart.pdb
C:\Projects\DirectPaymentSectionsWebPart\DirectPaymentSectionsWebPart\bin\Debug\Microsoft.Sharepoint.Sandbox.dll
C:\Projects\DirectPaymentSectionsWebPart\DirectPaymentSectionsWebPart\obj\Debug\ResolveAssemblyReference.cache
C:\Projects\DirectPaymentSectionsWebPart\DirectPaymentSectionsWebPart\obj\Debug\DirectPaymentSectionsWebPart.dll
C:\Projects\DirectPaymentSectionsWebPart\DirectPaymentSectionsWebPart\obj\Debug\DirectPaymentSectionsWebPart.pdb

新项目包含一个具有以下内容的同名文件:

C:\Projects\DirectPaymentSectionsWebPart\DirectPaymentSectionsWebPart\bin\Debug\DirectPaymentSectionsWebPart.dll
C:\Projects\DirectPaymentSectionsWebPart\DirectPaymentSectionsWebPart\bin\Debug\DirectPaymentSectionsWebPart.pdb
C:\Projects\DirectPaymentSectionsWebPart\DirectPaymentSectionsWebPart\bin\Debug\Microsoft.Sharepoint.Sandbox.dll
C:\Projects\DirectPaymentSectionsWebPart\DirectPaymentSectionsWebPart\obj\Debug\ResolveAssemblyReference.cache
C:\Projects\DirectPaymentSectionsWebPart\DirectPaymentSectionsWebPart\obj\Debug\DirectPaymentSectionsWebPart.dll
C:\Projects\DirectPaymentSectionsWebPart\DirectPaymentSectionsWebPart\obj\Debug\DirectPaymentSectionsWebPart.pdb
C:\Projects\DirectPaymentWebForm\DirectPaymentSectionsWebPart\obj\Debug\ResolveAssemblyReference.cache
C:\Projects\DirectPaymentWebForm\DirectPaymentSectionsWebPart\obj\Debug\DirectPaymentSectionsWebPart.dll
C:\Projects\DirectPaymentWebForm\DirectPaymentSectionsWebPart\bin\Debug\DirectPaymentSectionsWebPart.dll
C:\Projects\DirectPaymentWebForm\DirectPaymentSectionsWebPart\bin\Debug\DirectPaymentSectionsWebPart.pdb
C:\Projects\DirectPaymentWebForm\DirectPaymentSectionsWebPart\bin\Debug\Microsoft.Sharepoint.Sandbox.dll
C:\Projects\DirectPaymentWebForm\DirectPaymentSectionsWebPart\obj\Debug\DirectPaymentSectionsWebPart.pdb

所以我想知道:我是否应该从新项目的文件中删除引用旧项目的前六个条目?或者这样做会导致珍珠港三号爆发吗?

更新 2

我试过这个:

...事实证明此时已经有一个 ScriptManager (Page_Load()) 事件,并且它已经设置了 EnablePartialRendering,因此这段代码没有实际意义。但是,问题仍然是,为什么 Hec Ramsay 中的 CustomTitle 在这里为空:

if (this.dpsvisWebPart != null && this.dpsvisWebPart.CustomTitleProp != null)

...在 Page_Load() 事件中的 (about-to-be-removed-because-its-moot) 代码之后?

更新 3

好吧(或者是 "all right"?)- 无论如何,当显然并非一切都好时,这是一件有趣的事情,但话又说回来,今天是星期五,所以没关系.. .无论如何:

我想知道:"maybe 'CustomTitleProp' is null because the Web Editor (after all, that's where CustomTitleProp lives) is not being created/rendered. And sure enough, when I went to "编辑 Web 部件”Web 部件编辑器甚至不会显示,我再次被 red-hot、reeking-breath Sharepoint mother-in-law 因此:

"Web Part Error: A Web Part or Web Form Control on this Page cannot be displayed or imported. The type DirectPaymentSectionsWebPart.DPSVisualWebPart.DPSVisualWebPart, DirectPaymentSectionsWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=bbf47e850237632c could not be found or it is not registered as safe."

唉,又回到编码板了!顺便说一下,为什么我的 WebPartEditor 没有显示?

(抱歉,我最近一直在阅读 "A Connecticut Yankee in King Arthur's Court")。

我认为您必须在 Web.config 中将您的 dll 注册为安全的。

在 Web 部件编辑器中为此输入一个值后,问题就消失了。但问题仍然存在,如何在通过 Web 部件编辑器添加值之前 防止这种情况发生?我看不出有什么办法可以给它一个默认值;我假设即使 space (" ") 也能解决问题...

我可以这样做吗:

[WebBrowsable(true),
Category("Financial Affairs Forms"),
Personalizable(PersonalizationScope.Shared),
WebDisplayName("WebPart Title Text")]        
public string CustomTitleProp { call a custom method to return a space or something; set; }

?