簡體   English   中英

在自定義屬性上設置默認值

[英]Set default value on a custom property

我創建了一個longstring自定義屬性,該屬性為我提供了XHTML編輯器。 到目前為止,還不錯,但是我需要兩點幫助。

首先,我想用默認值填充屬性。 我看過幾篇關於此的博客文章,但似乎做得不好。

其次,我想將自定義屬性呈現為可以容納大字符串的常規textbox

public class CustomerTypeBoxControl :
    EPiServer.Web.PropertyControls.PropertyLongStringControl
{
    protected override void SetupEditControls()
    {
        base.SetupEditControls();                            
    }

    public CustomerTypeBox CustomerTypeBox
    {
        get
        {
            return PropertyData as CustomerTypeBox;
        }
    }
}

[Serializable]
[PageDefinitionTypePlugIn]
public class CustomerTypeBox : EPiServer.Core.PropertyLongString
{
    public override IPropertyControl CreatePropertyControl()
    {
        return new CustomerTypeBoxControl();
    }
}

不知道它是否與鋼鐵相關,但是以下是解決方案:

TextBox _textBox;
protected override void SetupEditControls()
{
    base.SetupEditControls();

    _textBox = (TextBox)EditControl;
    var value = CustomerTypeBox.Value ?? string.Empty;
    if (String.IsNullOrEmpty(value.ToString()))
    {
        _textBox.Text = "Default text";
    }
    else
    {
        _textBox.Text = value.ToString();
    }
    if (_textBox != null) EditControl.Parent.Controls.Add(_textBox);
}

public override void ApplyEditChanges()
{
    var customerTypeBoxValue = _textBox.Text;

    if (customerTypeBoxValue != null)
    {
        SetValue(customerTypeBoxValue);
    }
}

屬性的默認值也可以在管理模式下設置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM