Copied to clipboard

Flag this post as spam?

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

  • Tim 15 posts 35 karma points
    Jun 09, 2011 @ 03:32

    umbraco usercontrol wrapper save on postback

    Bit of a newbie question but I'm trying to create a custom data type using the umbraco usercontrol wrapper.

    Have the following form:

    Basically, you can see 3 rows. The first is to create new menu items, the 2nd and third are listings of the existing items. Buttons to the right allow you to save edits, move and delete these existing items.

    I can get everything to work as expected however each button press will only perform action within the current view state - if I want it to be permanent I have to press the default save button after each change for it to be permanent and written back to DB.

    My question is - how can I (or is it even possible to) declaratively call a method to save changes back to the DB on each of my custom button postbacks, rather than requiring the default save button?

    Thanks :)

  • Steven Newstead 62 posts 103 karma points
    Jun 09, 2011 @ 09:28
    Steven Newstead

    I think I understand what you mean, you just need to declare different click events for each of the buttons, so in your control you would have something like:


    <asp:Button id="UpButton"

    In the codebehind do something like this

    protected void UpBtn_Click(Object sender,
                               EventArgs e)
         //Do whatever you want to do with saving here.

    If this is not working then you might need to manually hook up the events, this is a simple little page that explains this:

    If this isn't what you mean then please let me know.




  • Tom Fulton 2030 posts 4963 karma points c-trib
    Jun 09, 2011 @ 14:16
    Tom Fulton

    In your click events I suppose you could save the properties manually using the Document API

    Document d = new Document(Convert.ToInt32(HttpContext.Current.Request.QueryString["id"]));
    d.getProperty("yourAlias").Value = "yourvalue";


  • Tim 15 posts 35 karma points
    Jun 10, 2011 @ 07:54

    Thank you Steven and Tom, kind of what I'm looking for but not quite...

    I had already implemented the button_OnClick methods as suggested, they're all working fine, updating the view etc

    The problem I'm having is that say for example I edit and existing item and click the save button to right of the row:

    This is the OnClick method...

    protected void EditItem(object sender, EventArgs e)
    var button = (Button)sender;
    var itemID = int.Parse(button.CommandArgument);
    var menuItems = GetMenuItemsFromXml(XmlValue.Value);

    var parentControl = button.Parent;
    menuItems.Where(x => x.ID == itemID).FirstOrDefault().Title = ((TextBox)parentControl.FindControl("ItemTitle")).Text;
    menuItems.Where(x => x.ID == itemID).FirstOrDefault().Description = ((TextBox)parentControl.FindControl("ItemDescription")).Text;
    menuItems.Where(x => x.ID == itemID).FirstOrDefault().Price = ((TextBox)parentControl.FindControl("ItemPrice")).Text;

    XmlValue.Value = GetXmlStringFromMenuItems(menuItems);

    BindRepeater(menuItems.OrderBy(x => x.ID).ToList());

    At the moment the view gets updated as expected however the Umbraco database value doesn't get updated and if I refresh the page, any changes since pressing the save button at the TOP of page are lost.

    Am I just missing an assignment of the Umbraco value in my OnClick method?

    Thank you again :)

  • Tom Fulton 2030 posts 4963 karma points c-trib
    Jun 10, 2011 @ 13:57
    Tom Fulton

    Hi Tim,

    So are you saying the data saves fine if you push Save, but you want it to save instead when you click the buttons in your usercontrol?  This is how datatypes are designed to work, but I was thinking you might be able to try the above snippet to save the value manually on your button clicks.


  • Steven Newstead 62 posts 103 karma points
    Jun 13, 2011 @ 15:44
    Steven Newstead

    I think Tom's suggestion sounds sensible

Please Sign in or register to post replies

Write your reply to: