Copied to clipboard

Flag this post as spam?

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


  • Simon Dingley 1470 posts 3427 karma points c-trib
    Mar 08, 2009 @ 16:15
    Simon Dingley
    0

    Custom DataType - DropDownList Value Being Lost

    I have created by first DataType and having problems as the value is not being saved and I am not sure why. When debugging I am getting an empty string value for umbracoValue every time.

    My CodeBehind is as follows and it is simply a dropdownlist of numeric values with a branch name as the display text:

    [code]using System;
    using System.Web.UI;

    using umbraco.editorControls.userControlGrapper;

    namespace PN.Umbraco.UserControls
    {
    public partial class BranchDropDownList : System.Web.UI.UserControl, IUsercontrolDataEditor
    {
    public string umbracoValue;

    protected void Page_PreRender(object sender, EventArgs e)
    {
    if (Page.IsPostBack)
    {
    umbracoValue = this.branchesDropDownList.SelectedValue;
    }
    else
    {
    if (umbracoValue != null)
    this.branchesDropDownList.SelectedValue = umbracoValue;
    }
    }

    public object value
    {
    get
    {
    return umbracoValue;
    }
    set
    {
    if (value != null)
    umbracoValue = value.ToString();
    }
    }
    }
    }[/code]

    Any help would be much appreciated.

  • Comment author was deleted

    Mar 08, 2009 @ 16:19

    Hi,

    When you debug,

    if (Page.IsPostBack)
    {
    umbracoValue = this.branchesDropDownList.SelectedValue;
    }

    What's the value of this.branchesDropDownList.SelectedValue ?

  • Comment author was deleted

    Mar 08, 2009 @ 16:31

    It might be because you are setting the umbracoValue in the PagePreRender, try moving to PageLoad

  • Simon Dingley 1470 posts 3427 karma points c-trib
    Mar 08, 2009 @ 23:29
    Simon Dingley
    0

    I did have it in Page_Load previously and the same problem existed, always the first item in the dropdownlist is reported as the selected item.

  • Simon Dingley 1470 posts 3427 karma points c-trib
    Mar 26, 2009 @ 11:33
    Simon Dingley
    0

    OK, I have an update on this problem but first here is my updated control source:

    [code] ///


    /// A control to display a list of branches
    ///

    public partial class BranchDropDownList : System.Web.UI.UserControl, IUsercontrolDataEditor
    {
    public string umbracoValue;

    ///
    /// Handles the Load event of the Page control.
    ///

    /// The source of the event.
    /// The instance containing the event data.
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();
    //- Load the branches.config file
    xmldoc.Load(Server.MapPath("~/data/branches.config"));
    //- LINQ query to get values from branch config since we are using .Net 3.5
    this.branchesDropDownList.Items.AddRange((from System.Xml.XmlElement el in xmldoc.SelectNodes("/*/branch")
    select new ListItem(el.GetAttribute("name"), el.GetAttribute("id"))).ToArray());
    this.branchesDropDownList.Items.Insert(0, "Please select...");

    }
    else
    {
    if (this.branchesDropDownList.SelectedIndex > 0)
    {
    umbracoValue = this.branchesDropDownList.SelectedValue;
    }
    }

    //- Set the selected branch in the list if it exists
    if (this.branchesDropDownList.Items.FindByValue(umbracoValue) != null)
    {
    this.branchesDropDownList.Items.FindByValue(umbracoValue.ToString()).Selected = true;
    }
    }

    public object value
    {
    get
    {
    return umbracoValue;
    }
    set
    {
    umbracoValue = value.ToString();
    }
    }
    }[/code]

    The control works fine and as intended on documents but fails when used as a member property. For a member that has a value already in the data the correct index is selected on first load but as soon as the member is saved the data is wiped out because for some reason umbracoValue is always null. I have been banging my head against a wall for quite some time on this now and got nowhere so I am hoping that someone may be able to assist or at least shed some light on where I am going wrong.

    Thanks, Simon

  • cp 21 posts 42 karma points
    Aug 21, 2009 @ 05:28
    cp
    0

    I am having the exact same problem. Has anyone figure out a solution? Thanks!

  • Masood Afzal 176 posts 522 karma points
    Sep 21, 2009 @ 14:42
    Masood Afzal
    0

    Simon,

    ViewState is enabled for your control?

    Try overriding ViewSatate functions and adding umbraco value


    protected override object SaveViewState()
    {
    ViewState["umbracoValue"]= umbracoValue;
    return base.SaveViewState();
    }


    protected override void LoadViewState(object savedState)
    {
    umbracoValue= ViewState["umbracoValue"];
    base.LoadViewState(savedState);
    }


     

  • Simon Dingley 1470 posts 3427 karma points c-trib
    Sep 23, 2009 @ 13:49
    Simon Dingley
    0

    Here is the url that pointed me to my solution:

    http://geekswithblogs.net/mikethomas/archive/2007/01/15/103686.aspx

    ..but more importantly a more recent thread on the same topic with a different solution, hopefully one or the other will help:

    http://our.umbraco.org/forum/developers/extending-umbraco/4230-Persisting-DropDownList-on-PostBack-(in-custom-sectiontree-page)

  • CASTELLI 23 posts 76 karma points
    Jun 19, 2013 @ 15:28
    CASTELLI
    0

    I was trying to create a custom checkboxlist datatype with a default selected value to be used in several places in Umbraco and in particular in the member section. I visited quite a number of forums and followed each different solution I found but none worked completely for me.

    I am using Umbraco v6.

    My main issue was in the "if (Page.IsPostBack)" of the Page_Load event where I was supposed to set the umbracoValue when the user saves its new selected values, the checkboxlist did not have these selected items as selected ! Instead I always had the values as defined in my ascx page, ie only the default value selected. There was no problem in displaying the saved values when loading the member's page.

    I changed the code and found a way to make it all work, but cannot explain why I had to do it this way when nearly every forum tells me otherwise. If someone can enlight me ... Thanks.

    here is the ascx code : @ControlLanguage="C#"AutoEventWireup="true"CodeBehind="s2k_dataType.ascx.cs"Inherits="WebUmbraco.s2k_dataType" %>

    <asp:CheckBoxListID="chkEnseignes"runat="server"AutoPostBack="True">
     
    <asp:ListItemSelected="True"Value="0">Sport 2000 Plaineasp:ListItem>
     
    <asp:ListItem Value="1">Sport 2000 Montagneasp:ListItem>
     
    <asp:ListItemValue="2">Mondovelo</asp:ListItem>
     
    <asp:ListItemValue="3">S2asp:ListItem>
     
    <asp:ListItemValue="4">Espace Montagneasp:ListItem>
     
    <asp:ListItemValue="5">Ski-Wayasp:ListItem>
     
    <asp:ListItemValue="6">Degrif' Sportasp:ListItem>
    asp:CheckBoxList>

    here is the ascx.cs code :
    publicstring umbracoValue;

    public object value
    {
          get
                {
                 foreach (ListItem item in chkEnseignes.Items)
                     {
                          if (item.Selected)
                         {
                              if (umbracoValue == null) umbracoValue = item.Value;
                              else umbracoValue += "," + item.Value;
               }//if
            }//foreach
                   return umbracoValue;
         }//get

             set
             {
             umbracoValue = value.ToString();
         }//set
     }

     protectedvoid Page_Load(object sender, EventArgs e)
    {
          if (!Page.IsPostBack)
          {
               if (umbracoValue != null)
               {
                       string[] selenseignes = umbracoValue.Split(',');
             chkEnseignes.SelectedIndex = -1;
             
    foreach (string sel in selenseignes)
                       {
                               ListItem item = chkEnseignes.Items.FindByValue(sel);
                
    if (item != null) item.Selected = true;
             }//foreach
         }//if
      }//if
    }//Page_Load

Please Sign in or register to post replies

Write your reply to:

Draft