【问题标题】:SharePoint 2013Visual WebPart with custom properties具有自定义属性的 SharePoint 2013 可视化 Web 部件
【发布时间】:2015-01-11 03:25:28
【问题描述】:

已解决。继续阅读...

我可以从MSDN aricle 1:1 复制代码,但它不起作用。 我在 Web 部件属性中看不到任何自定义属性。 虽然没有“杂项”类别。只有外观、布局和高级。

namespace Tts.CareersPortal.Web.CareersPortal.Fn.WebParts.NewContactTeaserWebPart
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Web.UI;
    using System.Web.UI.WebControls.WebParts;
    using MyNameSpace.NewContactTeaserWebPart;

    public partial class NewContactTeaserWebPartUserControl : UserControl
    {
        private int _contactTeaserCount;

        [Category("Extended Settings"),
        Personalizable(PersonalizationScope.Shared),
        WebBrowsable(true),
        WebDisplayName("Sample Text"),
        WebDescription("Please Enter a Sample Text")]
        public int ContactTeaserTeaserCount
        {
            get
            {
                return _contactTeaserCount;
            }
            set
            {
                _contactTeaserCount = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            IEnumerable<MyContact> contacts = LoadContactProperties();

            Repeater1.DataSource = contacts;
            Repeater1.DataBind();
        }

        private IEnumerable<MyContact> LoadContactProperties()
        {
            ContactFinder contactFinder = new ContactFinder(ContactTeaserTeaserCount);

            IEnumerable<MyContact> result = contactFinder.GetContact();
            return result;
        }
    }
}

解决方案:我将 Web 部件属性添加到错误的类文件中:-/ 您必须将其添加到 Web 部件 - 而不是用户控件。要将 webpart 属性传递给用户控件,我推荐 this 以下方法 - 其他方法也可以。

不起作用。有什么想法吗?

【问题讨论】:

    标签: sharepoint web-parts


    【解决方案1】:

    如果您在使用 C# 在 SharePoint 2010 中创建可视化 Web 部件方面需要帮助,请关注 this link

    如果您仅需要有关 webpart 属性的帮助,请尝试以下代码:

    public static string SampleText;
    [Category("Extended Settings"),
    Personalizable(PersonalizationScope.Shared),
    WebBrowsable(true),
    WebDisplayName("Sample Text"),
    WebDescription("Please Enter a Sample Text")]
    public string _SampleText
    {
        get { return SampleText; }
        set
        {
            // Sample Validation
            Regex oRegEx = new Regex("[a-zA-Z]+");
            if (!oRegEx.IsMatch(value))
                throw new Microsoft.SharePoint.WebPartPages.
                    WebPartPageUserException(
                    "Please enter alphabeth characters only");
            SampleText = value;
        }
    }
    

    还发现以下文章对此有帮助: Custom webpart properties

    【讨论】:

    • 啊! - 代码项目链接有帮助。我将属性添加到错误的类文件中:sharemuch.com/2011/11/17/…。
    【解决方案2】:

    WebBrowsable(true),使其在 webpart 属性中可见。 而这一个 Personalizable(PersonalizationScope.Shared) 定义了它将以何种模式(共享或个性化)可见。

    【讨论】:

      猜你喜欢
      • 2014-07-09
      • 1970-01-01
      • 2013-10-29
      • 2012-03-24
      • 2014-11-08
      • 2015-08-02
      • 2013-10-02
      • 2016-05-22
      • 2014-06-17
      相关资源
      最近更新 更多