Persisting DropDownList on PostBack (in custom section/tree page)
Hi all,
I am working on a custom section/tree, in which the tree nodes link through an ASPX page (inherited from the UmbracoEnsuredPage base-page).
On the form, I am populating a couple of DropDownList controls, with some node and member ids. This is fine.
But on PostBack, the DropDownList controls have lost all the ListItem options ... not even the SelectedValue! Most other controls are working fine (like TextBox). Before you ask, yes ViewState is enabled. ;-)
My only thought is that something in the UmbracoEnsuredPage isn't being raised in the event bubble?
I'm using the same concept in UmbImport so that shouldn't be an issue. Do your retrieve the data for the controls from a database? If so make sure that you databind on postback
Yes please share the code , must be something small. The selectedValue is gone because the list don't have any items and since the selected value is based on the control tree the selectedvalue is also gone.
Lee - Are you trying to access the data before it has been loaded from the ViewState. The Init event is when this occurs (see http://msdn.microsoft.com/en-us/library/ms178472.aspx for the ASP.NET Page Life Cycle) so if you're using PreInit or anything before that you'll run into a snag.
This is the url that helped me overcome the issue I was having on the project we were working on and mentioned in the thread Ismail has linked to above:
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" %>
Persisting DropDownList on PostBack (in custom section/tree page)
Hi all,
I am working on a custom section/tree, in which the tree nodes link through an ASPX page (inherited from the UmbracoEnsuredPage base-page).
On the form, I am populating a couple of DropDownList controls, with some node and member ids. This is fine.
But on PostBack, the DropDownList controls have lost all the ListItem options ... not even the SelectedValue! Most other controls are working fine (like TextBox). Before you ask, yes ViewState is enabled. ;-)
My only thought is that something in the UmbracoEnsuredPage isn't being raised in the event bubble?
Any ideas? suggestions? I'm literally stuck!
Thanks much,
- Lee
Hi Lee,
I'm using the same concept in UmbImport so that shouldn't be an issue. Do your retrieve the data for the controls from a database? If so make sure that you databind on postback
Cheers,
Richard
Thanks Richard. I did try to rebind the datasource to the DropDownList control (for example, I am pulling back a list of members via the API).
But since the DropDownList had already lost the SelectedValue property, I'm not sure what to do now?
I haven't got the code with me at the moment, but I will post an example later on today.
Thanks again!
- Lee
Hi Lee,
Yes please share the code , must be something small. The selectedValue is gone because the list don't have any items and since the selected value is based on the control tree the selectedvalue is also gone.
Cheers,
Richard
Lee,
This any good someone else having same issue although in datatype.
Regards
Ismail
Lee - Are you trying to access the data before it has been loaded from the ViewState. The Init event is when this occurs (see http://msdn.microsoft.com/en-us/library/ms178472.aspx for the ASP.NET Page Life Cycle) so if you're using PreInit or anything before that you'll run into a snag.
Another thing, is the DropDownList enabled when the postback occurs? See this post I did - http://www.aaron-powell.com/blog/january-2009/not-getting-dropdownlist-value-when-setting-it-via-javascript.aspx on the the topic
Hi Lee,
This is the url that helped me overcome the issue I was having on the project we were working on and mentioned in the thread Ismail has linked to above:
http://geekswithblogs.net/mikethomas/archive/2007/01/15/103686.aspx
And uses the EnsureDataBound() event to rebind the data.
OK, finally it works! Woo hoo! Thanks to all for pointing me in the right direction!
@Richard, you were right, because the DropDownList wasn't being rebound to the data, but I was trying to do it as Page_Load, which didn't work.
@slace, it was your Page_Init suggestion that worked for me. Now I am populating the DropDownList controls there, rather than in Page_Load.
.. all is good now. Here's that code snippet I promised earlier.
Now on Postback, the DropDownList control has all it's ListItem options and the ViewState is correctly loading in the SelectedValue!
Thanks again for everyone's help!
- Lee
Psst - you really should use the ASP.NET Membership API to get the list of members ;)
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...