Copied to clipboard

Flag this post as spam?

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


  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Apr 17, 2009 @ 17:29
    Jeroen Breuer
    0

    Create node with a Datatype

    Hello,

    I want to create a node under the current node. I’m trying to do this by creating a custom datatype using the umbraco usercontrol wrapper. I tried it with an example I found on nibble (http://www.nibble.be/?p=24) but I can’t get it working.



    When I press the save button none of the values are saved and I get to see the original values. Does anybody know what I’m doing wrong or what I should do to can create a node with a Datatype? I found another topic (http://forum.umbraco.org/yafpostst5907Best-way-to-move-a-node-using-NET-data-type.aspx), but that’s not exactly what I’m looking for.

    Here is the source code:

    BeheerNode.ascx
    [code]
    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="BeheerNode.ascx.cs" Inherits="UserControls.Datatypes.BeheerNode" %>




    [/code]

    BeheerNode.ascs.cs
    [code]
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    using umbraco.presentation.nodeFactory;

    namespace UserControls.Datatypes
    {
    public partial class BeheerNode : System.Web.UI.UserControl, umbraco.editorControls.userControlGrapper.IUsercontrolDataEditor
    {
    public string umbracoValue;

    protected void PageLoad(object sender, EventArgs e)
    {
    if (Page.IsPostBack)
    {
    }
    }

    protected void BtnSave
    Click(object sender, EventArgs e)
    {
    string value1 = TxtNaam.Text;
    string value2 = TxtNaam2.Text;
    string value3 = TxtNaam3.Text;
    }

    #region IUsercontrolDataEditor Members

    public object value
    {
    get
    {
    return umbracoValue;
    }
    set
    {
    umbracoValue = value.ToString();
    }
    }

    #endregion
    }
    }
    [/code]

    Hope someone can help me.

  • Comment author was deleted

    Apr 17, 2009 @ 17:32

    What are you trying to do ? Because your code doesn't look like it will do something

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Apr 17, 2009 @ 21:50
    Jeroen Breuer
    0

    [quote=tim]What are you trying to do ? Because your code doesn't look like it will do something[/quote]

    The code isn't finished because I can't even get this to work. When I enter a new value in the Nodename222 field and then press the save button I want to read the new value into a string.

    [code]
    protected void BtnSave_Click(object sender, EventArgs e)
    {
    string value1 = TxtNaam.Text;
    string value2 = TxtNaam2.Text;
    string value3 = TxtNaam3.Text;
    }

    [/code]

    This even doensn't work yet and the TxtNaam.Text is still the previous value (Nodename222) instead of the new value I entered in the TextBox. Once that works I want to create a new node and give the name of the node the value I entered in the TextBox.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Apr 20, 2009 @ 16:10
    Jeroen Breuer
    0

    I've discovered some things. When I press my own save button which is part of the user control the textbox values are read correctly. Only when the save button of umbraco is pressed the textbox values aren't read correctly and display the pervious value.



    Here is my current source code:

    [code]
    protected void BtnSaveClick(object sender, EventArgs e)
    {
    string value1 = TxtNaam.Text;

    int id = 0;
    if (int.TryParse(Request.QueryString["id"], out id))
    {
    int userId = umbraco.BasePages.UmbracoEnsuredPage.GetUserId(umbraco.BasePages.UmbracoEnsuredPage.umbracoUserContextID);
    DocumentType typeToCreate = DocumentType.GetByAlias("formulier");
    Document newDoc = Document.MakeNew(value1, typeToCreate, new global::umbraco.BusinessLogic.User(userId), id);
    newDoc.Publish(new global::umbraco.BusinessLogic.User(userId));
    library.RefreshContent();
    library.UpdateDocumentCache(newDoc.Id);
    }
    }
    [/code]

    Currently only the first textbox is functional. When the save button is pressed a new node is created and published under the current node. I've used a part of the umbraco sourcecode to create the node.

    I would like this code to also work correctly when the umbraco save button is pressed. After the new node is created I would like to update the content tree so the new node can be displayed immediatly. More info about that can be found at http://forum.umbraco.org/yaf
    postsm44686_How-to-reload-the-content-tree.aspx.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Apr 20, 2009 @ 17:00
    Jeroen Breuer
    0

    Because I can't get the save button of umbraco to work yet I'm trying a workaround. The umbraco save button probably doens't work because the order of the page-load. I fount this information at http://forum.umbraco.org/yafpostst2249Textbox-in-Usercontrol-Wrapper-not-working-.aspx. In the workaround I want to read the values of my usercontrol in the Document.BeforeSave or Document.AfterSave event. In this event I want to use the FindControl method to find my textboxes and read the values there (which should be the correct value by then). The only problem is I can't find my usercontrol in the event. Here is my (incomplete) soure code so far:

    [code]
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    using umbraco.BusinessLogic;
    using umbraco.cms.businesslogic;
    using umbraco.cms.businesslogic.property;
    using umbraco.cms.businesslogic.member;
    using umbraco.cms.businesslogic.web;


    namespace TribalUmbraco
    {
    public class TribalEvents : ApplicationBase
    {
    public TribalEvents()
    {
    Document.BeforeSave += new Document.SaveEventHandler(DocumentBeforeSave);
    Document.AfterSave += new Document.SaveEventHandler(Document
    AfterSave);
    }

    protected void DocumentBeforeSave(Document sender, SaveEventArgs e)
    {
    Property[] properties = sender.getProperties;
    Control textControl = properties[1].PropertyType.DataTypeDefinition.DataType.DataEditor.Editor.FindControl("TxtNaam");
    if (textControl != null && textControl is TextBox)
    {
    string tekst = ((TextBox)textControl).Text;
    }
    }

    protected void Document
    AfterSave(Document sender, SaveEventArgs e)
    {
    ;
    }
    }
    }

    [/code]

    With debugging I discoverd properties[1] is my usercontrol datatype, but the Control textControl is always null because properties[1].PropertyType.DataTypeDefinition.DataType.DataEditor.Editor. has 0 controls. In the umbraco source code I looked for the usercontrolDataType and found the following code:

    [code]
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    namespace umbraco.editorControls.userControlGrapper
    {
    public class usercontrolDataType : umbraco.cms.businesslogic.datatype.BaseDataType, umbraco.interfaces.IDataType
    {
    private umbraco.interfaces.IDataEditor Editor;
    private umbraco.interfaces.IData _baseData;
    private usercontrolPrevalueEditor _prevalueeditor;

    public override umbraco.interfaces.IDataEditor DataEditor
    {
    get
    {
    if (
    Editor == null)
    Editor = new usercontrolDataEditor(Data, ((usercontrolPrevalueEditor) 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("D15E1281-E456-4b24-AA86-1DDA3E4299D5"); }
    }

    public override string DataTypeName
    {
    get { return "umbraco usercontrol wrapper"; }
    }

    public override umbraco.interfaces.IDataPrevalue PrevalueEditor
    {
    get
    {
    if (
    prevalueeditor == null)
    _prevalueeditor = new usercontrolPrevalueEditor(this);
    return _prevalueeditor;
    }
    }
    }
    }
    [/code]

    It looks like it always returns a usercontrolPrevalueEditor so is it even possible to get my usercontrol controls the way I'm trying now?

  • Comment author was deleted

    Apr 20, 2009 @ 17:10

    Hi, when using the usercontrol wrapper method you need to implement to code that needs to be executed when the save button of umbraco is used during the postback.

    So


    protected void Page_Load(object sender, EventArgs e)
    {
    if (Page.IsPostBack)
    {
    //here
    }
    }

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Apr 20, 2009 @ 17:13
    Jeroen Breuer
    0

    [quote=tim]Hi, when using the usercontrol wrapper method you need to implement to code that needs to be executed when the save button of umbraco is used during the postback.

    So


    protected void PageLoad(object sender, EventArgs e)
    {
    if (Page.IsPostBack)
    {
    //here
    }
    }[/quote]

    I've already tried that.

    [code]
    protected void Page
    Load(object sender, EventArgs e)
    {
    if (Page.IsPostBack)
    {
    string value1 = TxtNaam.Text;
    string value2 = TxtNaam2.Text;
    string value3 = TxtNaam3.Text;
    }
    }
    [/code]

    When my own save button is pressed this works and I get the values I entered in the textbox. When I press the umbraco save button I get the previous values and not the ones I entered.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Apr 22, 2009 @ 09:06
    Jeroen Breuer
    0

    Doesn't anybody know how to solve my problems?

  • Gareth 20 posts 24 karma points
    Jun 11, 2009 @ 23:35
    Gareth
    0

    I am experiencing this too where the value I select from a custom drop down list (in an IUsercontrolDataEditor user control that represents an umbraco data type) is not set when the PageLoad() of the user control fires after I click Save. It's always the default value

    When I inspect the request parameters I can see the value of the drop down list in Request["ctl00$body$ctl39$DropDownList
    FavouriteColor"] but for some reason this isn't getting through to my user control on PageLoad()

    Note: The drop down list has ID="FavouriteColour".

    Any ideas what's wrong? I expect it to be a really simple problem

    The .ascx :

    [code]<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="FavouriteColour.ascx.cs" Inherits="UmbracoTestObjects.UserControls.FavouriteColour" %>
    [/code]


    The ascx.cs :

    [code]using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using umbraco.editorControls;

    namespace UmbracoTestObjects.UserControls
    {
    public partial class FavouriteColour : System.Web.UI.UserControl
    , umbraco.editorControls.userControlGrapper.IUsercontrolDataEditor
    {
    public object value { get; set; }

    public FavouriteColour()
    {
    //value = "";
    }

    protected void Page
    Load(object sender, EventArgs e)
    {
    if (Page.IsPostBack == true)
    {
    value = DropDownListFavouriteColor.SelectedValue;
    }
    else
    {
    if (value == System.DBNull.Value)
    {
    value = "";
    }
    else if( ((string)value) != "")
    {
    ListItem oListItem = null;

    for( int nIndex = 0, nCount = DropDownList
    FavouriteColor.Items.Count;
    nIndex < nCount; nIndex ++)
    {
    oListItem = DropDownList_FavouriteColor.Items[nIndex];

    if( oListItem.Value == (string)value)
    {
    oListItem.Selected = true;
    }
    }
    }
    }
    }
    }
    }[/code]

  • Gareth 20 posts 24 karma points
    Jun 12, 2009 @ 00:00
    Gareth
    0

    Fixed it. Found the answer in http://forum.umbraco.org/yafpostst5944Custom-datatype-usercontrol-wrapper-problem.aspx thanks to rsoeteman for highlighting the obvious

    To clarify, a DropDownList's value isn't populated in the asp.net page lifecycle until the control events are fired which occurs after the page load event.

    I moved my code from PageLoad to DropDownListFavouriteColorSelectedIndexChanged and it worked as expected

    [code]protected void DropDownList
    FavouriteColorSelectedIndexChanged(object sender, EventArgs e)
    {
    umbracoValue = DropDownList
    FavouriteColor.SelectedValue;
    }[/code]

    So easy it hurts

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jun 12, 2009 @ 09:45
    Jeroen Breuer
    0

    That works indeed, but the webcontrol should have an event which you can call and that's not always the case. Now I just use my own save button because that way everything works.

  • Gareth 20 posts 24 karma points
    Jun 13, 2009 @ 11:17
    Gareth
    0

    [quote=jbreuer]That works indeed, but the webcontrol should have an event which you can call and that's not always the case. Now I just use my own save button because that way everything works.[/quote]

    I got it working by moving the code to PagePreRender() in the user control eg:

    [code]protected void Page
    PreRender(object sender, EventArgs e)
    {
    string value1 = TxtNaam.Text;
    string value2 = TxtNaam2.Text;
    string value3 = TxtNaam3.Text;

    int id = 0;
    if (Page.IsPostBack == true && int.TryParse(Request.QueryString["id"], out id))
    {
    int userId = umbraco.BasePages.UmbracoEnsuredPage.GetUserId(umbraco.BasePages.UmbracoEnsuredPage.umbracoUserContextID);
    DocumentType typeToCreate = DocumentType.GetByAlias("DateFolder");
    Document newDoc = Document.MakeNew(value1, typeToCreate, new global::umbraco.BusinessLogic.User(userId), id);
    newDoc.Publish(new global::umbraco.BusinessLogic.User(userId));
    library.RefreshContent();
    library.UpdateDocumentCache(newDoc.Id);
    }
    }[/code]

    At least it creates a child node of the current node which I think is what you want to do. It doesn't feel right in terms of UI though I think your Save button to perform the action is better anyway from a user point of view.

    It might be even better to have this functionality as an action by right clicking the node? You could popup a dialog with the three text boxes on and do it that way? Having it on the node edit screen seems wrong (to me) as it's not data editing

    I think it would be nice to have a context sensitive toolbar in the Content screen? Maybe it's possible I don't know yet

  • Gareth 20 posts 24 karma points
    Jun 13, 2009 @ 11:18
    Gareth
    0

    Also I wonder if there is a way to invoke Reload Nodes so that the new node appears after pressing Save

  • Gareth 20 posts 24 karma points
    Jun 13, 2009 @ 12:02
    Gareth
    0

    One more thing. I tested a similar user control outside of Umbraco with two submit buttons (one on the default.aspx and one on the user control) and as expected the text controls' values were set right from the first breakpoint in page_load unlike when they're hosted in umbraco. Not sure why at the moment

Please Sign in or register to post replies

Write your reply to:

Draft