具有自定义属性的SharePoint 2013 Visual Web部件

时间:2014-11-13 20:47:32

标签: sharepoint web-parts

解决。请继续阅读......

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

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部件属性添加到错误的类文件中: - /您必须将其添加到webpart - 而不是用户控件。要将webpart属性传递给用户控件,我建议this采用以下方法 - 其他方法也可以使用。

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

2 个答案:

答案 0 :(得分:0)

如果您在使用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

答案 1 :(得分:0)

WebBrowsable(true),使其在webpart属性中可见。 这个Personalizable(PersonalizationScope.Shared)定义了什么模式(共享或个性化)它将是可见的。