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;
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...");
//- 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.
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" %>
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
Hi,
When you debug,
if (Page.IsPostBack)
{
umbracoValue = this.branchesDropDownList.SelectedValue;
}
What's the value of this.branchesDropDownList.SelectedValue ?
Comment author was deleted
It might be because you are setting the umbracoValue in the PagePreRender, try moving to PageLoad
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.
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
I am having the exact same problem. Has anyone figure out a solution? Thanks!
Simon,
ViewState is enabled for your control?
Try overriding ViewSatate functions and adding umbraco value
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)
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
is working on a reply...