Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Sherry Ann Hernandez 320 posts 344 karma points
    Mar 23, 2011 @ 09:25
    Sherry Ann Hernandez
    0

    Custom datatype value does not display upon saving

    Hi,

    I create a custom datatype for the telephone number that consist of three text box for the country code, area code and phone number.

    The problem is I can see that it is saving in the database when I check the cmsPropertyData table. But when I try to load again the content that have that custom data type the textboxes are all empty. I'm not sure what is happening on it.

    Here's my code.

    Datatype.cs

    public

     

     

    class DataType : umbraco.cms.businesslogic.datatype.AbstractDataEditor

    {

     

     

    private ContactNumberControl control = new ContactNumberControl();

     

     

    // Set ID, needs to be unique

     

     

    public override Guid Id

    {

     

     

    get

    {

     

     

    return new Guid("0785EF73-E138-4A11-B03B-4E39B06B5229");

    }

    }

     

     

    //Set name, (is what appears in data editor dropdown)

     

     

    public override string DataTypeName

    {

     

     

    get

    {

     

     

    return "Contact Number (using data editor settings)";

    }

    }

     

     

    public DataType()

    {

     

     

    //set rendercontrol

     

     

    base.RenderControl = control;

     

     

    //init event

    control.Init +=

     

    new EventHandler(control_Init);

     

     

    //save event

     

     

    base.DataEditorControl.OnSave += new umbraco.cms.businesslogic.datatype.AbstractDataEditorControl.SaveEventHandler(DataEditorControl_OnSave);

    }

     

     

    void control_Init(object sender, EventArgs e)

    {

     

    control.Text =

     

    base.Data.Value != null ? base.Data.Value.ToString() : "";

    }

     

     

    void DataEditorControl_OnSave(EventArgs e)

    {

     

     

    base.Data.Value = control.Text;

    }

     

     

     

       ContactNumber.cs

    [

     

    ValidationProperty("Text")]

     

     

    public class ContactNumberControl : Panel

    {

     

     

    private TextBox txtCountryCode;

     

     

    private TextBox txtAreaCode;

     

     

    private TextBox txtPhone;

     

     

    public string Text

    {

     

     

    get

    {

     

     

    return txtCountryCode.Text + "-" + txtAreaCode.Text + "-" + txtPhone.Text;

    }

     

     

    set

    {

     

     

    //if (value != null)

     

     

    //{

     

     

    string[] arrValue = value.Split('-');

     

     

     

    //if (txtCountryCode == null)

     

     

    // txtCountryCode = new TextBox();

    txtCountryCode.Text = arrValue[0];

     

     

    //if (txtAreaCode == null)

     

     

    // txtAreaCode = new TextBox();

    txtAreaCode.Text = arrValue[1];

     

     

    //if (txtPhone == null)

     

     

    // txtPhone = new TextBox();

    txtPhone.Text =

     

    value;

     

     

    //}

    }

    }

     

     

    protected override void OnInit(EventArgs e)

    {

     

     

    base.OnInit(e);

    EnsureChildControls();

    }

     

     

    protected override void CreateChildControls()

    {

     

     

    base.CreateChildControls();

    txtCountryCode =

     

    new TextBox();

    txtCountryCode.MaxLength = 3;

    txtCountryCode.Width =

     

    Unit.Pixel(25);

    txtCountryCode.CssClass =

     

    "umbEditorTextField";

    txtCountryCode.Attributes.Add(

     

    "onkeyup", "this.value=this.value.replace(/[^\\d]/,'')");

    txtAreaCode =

     

    new TextBox();

    txtAreaCode.MaxLength = 3;

    txtAreaCode.Width =

     

    Unit.Pixel(25);

    txtAreaCode.CssClass =

     

    "umbEditorTextField";

    txtAreaCode.Attributes.Add(

     

    "onkeyup", "this.value=this.value.replace(/[^\\d]/,'')");

    txtPhone =

     

    new TextBox();

    txtPhone.MaxLength = 10;

    txtPhone.Width =

     

    Unit.Pixel(50);

    txtPhone.CssClass =

     

    "umbEditorTextField";

    txtPhone.Attributes.Add(

     

    "onkeyup", "this.value=this.value.replace(/[^\\d]/,'')");

     

     

    this.Controls.Add(new LiteralControl("<span>+&nbsp;</span>"));

     

     

    this.Controls.Add(txtCountryCode);

     

     

    this.Controls.Add(new LiteralControl("<span>&nbsp;-&nbsp;</span>"));

     

     

    this.Controls.Add(txtAreaCode);

     

     

    this.Controls.Add(new LiteralControl("<span>&nbsp;-&nbsp;</span>"));

     

     

    this.Controls.Add(txtPhone);

    }

     

     

    }

  • Chris Randle 85 posts 199 karma points c-trib
    Jun 12, 2012 @ 18:36
    Chris Randle
    1

    You don't appear to be actually saving the current document.  I suggest you try and instantiate the document first. Usually, something along the lines of the following works:

     

    protected void MyTextBox_TextChanged(object sender, EventArgs e)

    {

    int id;

            if (!int.TryParse(Request.QueryString["id"], out id) || String.IsNullOrEmpty(MyTextBox .Text))

            {

                throw new Exception("Invalid Document ID in querystring.");

            }

            

            var document = new Document(id);

            umbraco.library.UpdateDocumentCache(document.Id);

            document.getProperty("myPropertyAlias").Value =  MyTextBox.Text;

            document.Save();

     

    }

     

     

    Your example shows you assigning the values, but not persisting them.  Umbraco doesn't auto-persist AFAIK.

Please Sign in or register to post replies

Write your reply to:

Draft