Umbraco 4.5.2 - 创建自定义umbraco数据编辑器设置类型

时间:2011-02-17 08:27:37

标签: c# asp.net umbraco

在umbraco 6.2(Juno)之前,我似乎无法找到有关如何编辑数据编辑器设置的任何内容。有没有简单的方法,它必须是可能的。如果你不明白我的意思我想和http://www.nibble.be/?p=96做同样的事 - 只是对于umbraco 4.5.2。

谢谢:)

3 个答案:

答案 0 :(得分:2)

你需要制作3个Class Class 1 DataEditor

 public class DataEditor : System.Web.UI.UpdatePanel, umbraco.interfaces.IDataEditor
    {

        public MWCropperDataEditor(umbraco.interfaces.IData Data, string Configuration)
        {
            _data = Data;

        }

        public virtual bool TreatAsRichTextEditor
        {
            get { return false; }
        }

        public bool ShowLabel
        {
            get { return true; }
        }

        public Control Editor { get { return this; } }


        public void Save()
        {

                this._data.Value = "data;

        }

        protected override void OnInit(EventArgs e)
        {

            base.OnInit(e);

            imageUpload = new FileUpload();
            imageUpload.ID = "imageUpload";

            //shows Image
            cropImage = new System.Web.UI.WebControls.Image();
            cropImage.Width = width;
            cropImage.Height = height;
            cropImage.ImageUrl = this._data.Value.ToString();


            //Shows dropdown
            locationDropDown = new DropDownList();
            AddItemsToDropDown();

            lblInfo = new Label();
            lblInfo.Attributes.Add("id", "title" + base.ClientID);
            lblCropInfo = new Label();

            lblCropInfo.Text = "Crop Location: ";
            base.ContentTemplateContainer.Controls.Add(lblInfo);
            base.ContentTemplateContainer.Controls.Add(imageUpload);
            base.ContentTemplateContainer.Controls.Add(new LiteralControl("<br/>"));
            base.ContentTemplateContainer.Controls.Add(new LiteralControl("<br/>"));
            base.ContentTemplateContainer.Controls.Add(lblCropInfo);
            base.ContentTemplateContainer.Controls.Add(locationDropDown);
            base.ContentTemplateContainer.Controls.Add(new LiteralControl("<br/>"));
            base.ContentTemplateContainer.Controls.Add(new LiteralControl("<br/>"));
            base.ContentTemplateContainer.Controls.Add(cropImage);




        }

    }

class 2 DataType

public class MWCropperDataType : umbraco.cms.businesslogic.datatype.BaseDataType, umbraco.interfaces.IDataType
    {
        private umbraco.interfaces.IDataEditor _Editor;
        private umbraco.interfaces.IData _baseData;
        private MWCropperPrevalueEditor _prevalueeditor;

        public override umbraco.interfaces.IDataEditor DataEditor
        {
            get
            {
                if (_Editor == null)
                    _Editor = new MWCropperDataEditor(Data, ((MWCropperPrevalueEditor)PrevalueEditor).Configuration);
                return _Editor;
            }
        }

        public override umbraco.interfaces.IData Data
        {
            get
            {
                if (_baseData == null)
                    _baseData = new umbraco.cms.businesslogic.datatype.DefaultData(this);
                return _baseData;
            }
        }
        public override Guid Id
        {
            get { return new Guid("71518B4E-B1A5-11DD-A22C-8AAA56D89593"); }
        }

        public override string DataTypeName
        {
            get { return "MWCropper"; }
        }

        public override umbraco.interfaces.IDataPrevalue PrevalueEditor
        {
            get
            {
                if (_prevalueeditor == null)
                    _prevalueeditor = new MWCropperPrevalueEditor(this);
                return _prevalueeditor;
            }
        }
    }

Class 3 PrevalueEditor

public class MWCropperPrevalueEditor : System.Web.UI.WebControls.PlaceHolder, umbraco.interfaces.IDataPrevalue
    {
        #region IDataPrevalue Members

        // referenced datatype
        private umbraco.cms.businesslogic.datatype.BaseDataType _datatype;


        private TextBox _txtWidth;
        private TextBox _txtHeight;
        public MWCropperPrevalueEditor(umbraco.cms.businesslogic.datatype.BaseDataType DataType)
        {

            _datatype = DataType;
            setupChildControls();

        }

        private void setupChildControls()
        {

            _txtWidth = new TextBox();
            _txtWidth.ID = "txtWidth";
            _txtWidth.CssClass = "umbEditorTextField";
            Controls.Add(_txtWidth);
            _txtHeight = new TextBox();
            _txtHeight.ID = "txtHeight";
            _txtHeight.CssClass = "umbEditorTextField";
            Controls.Add(_txtHeight);


        }



        public Control Editor
        {
            get
            {
                return this;
            }
        }


        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!Page.IsPostBack)
            {

                if (Configuration.Length > 0)
                {
                    string[] value = Configuration.Split(new char[]{';'});
                    _txtWidth.Text = value[0];
                    _txtHeight.Text = value[1];

                }
                else
                {
                    _txtHeight.Text = "100";
                    _txtWidth.Text = "100";
                }


            }


        }

        public void Save()
        {
            _datatype.DBType = (umbraco.cms.businesslogic.datatype.DBTypes)Enum.Parse(typeof(umbraco.cms.businesslogic.datatype.DBTypes), DBTypes.Ntext.ToString(), true);


            string data = _txtWidth.Text+";"+_txtHeight.Text;

            SqlHelper.ExecuteNonQuery("delete from cmsDataTypePreValues where datatypenodeid = @dtdefid", 
                    SqlHelper.CreateParameter("@dtdefid", _datatype.DataTypeDefinitionId));
            SqlHelper.ExecuteNonQuery("insert into cmsDataTypePreValues (datatypenodeid,[value],sortorder,alias) values (@dtdefid,@value,0,'')", 
                    SqlHelper.CreateParameter("@dtdefid", _datatype.DataTypeDefinitionId), SqlHelper.CreateParameter("@value", data));

        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.WriteLine("<table>");
            writer.Write("<tr><th>Width:</th><td>");
            _txtWidth.RenderControl(writer);
            writer.Write("</td></tr>");
            writer.Write("<tr><th>Height:</th><td>");
            _txtHeight.RenderControl(writer);
            writer.Write("</td></tr>");
            writer.Write("</table>");
        }

        public string Configuration
        {
            get
            {
                object conf =
                   SqlHelper.ExecuteScalar<object>("select value from cmsDataTypePreValues where datatypenodeid = @datatypenodeid",
                                           SqlHelper.CreateParameter("@datatypenodeid", _datatype.DataTypeDefinitionId));

                if (conf != null)
                    return conf.ToString();
                else
                    return "";

            }
        }

        #endregion

        public static ISqlHelper SqlHelper
        {
            get
            {
                return Application.SqlHelper;
            }
        }
    }

我希望这可以帮助你开始:) 顺便说一句,这也适用于umbraco 6.2

答案 1 :(得分:1)

设置称为prevalues,您需要一个实现IDataPrevalue的PrevalueEditor类。看看这篇博文中的一个例子:

http://www.eyecatch.no/blog/my-first-umbraco-datatype---part-2-rendering-a-recaptcha-control.aspx

答案 2 :(得分:0)

这个蚕食帖子关注的是为v4.5和之前做过类似的事情,在这个版本中还有一个链接用于我之前关注的更老版本,并且发现非常有帮助。 http://www.nibble.be/?p=62