为什么这个String保持为空?

时间:2015-05-28 23:02:34

标签: c# sharepoint-2010

我的WebPart上的控件未创建,因为条件为:

if (this.dpsvisWebPart != null && this.dpsvisWebPart.CustomTitleProp != null)
由于CustomTitleProp为null,

...失败。然而,它是在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在前面提到的那个时候是null?这里的代码有更多的上下文:

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());
        . . .

更新

在“文件夹差异中的冒险”标题下出现:

旧项目包含一个名为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

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

更新2

我试过了:

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

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

...就在Page_Load()事件中的(即将删除的因为它的模拟代码)代码之后?

更新3

好吧(或者说“好吧”?) - 无论如何,这显然是一件很有趣的事情,显然不是一切都很好,但是再一次,这是星期五,所以没关系......无论如何:

我想知道:“也许'CustomTitleProp'是空的,因为Web编辑器(毕竟,那就是CustomTitleProp所在的地方)没有被创建/渲染。当然,当我去”编辑Web部件“Web部件编辑器时甚至不会显示,我再次被炽热的,令人窒息的Sharepoint婆婆所抨击:

"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."
唉,唉,它再次回到编码板!对于forsooth,为什么我的WebPartEditor不显示?

(对不起,我最近一直在阅读“亚瑟王法院的康涅狄格州洋基队”)。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

一旦我在Web部件编辑器中输入了一个值,问题就消失了。但问题仍然存在,如何在通过Web部件编辑器添加值之前阻止此?我认为没办法给它一个默认值;我假设即使是一个空间(&#34;&#34;)也可以做到这一点......

我可以这样做:

[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; }