Copied to clipboard

Flag this post as spam?

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


  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Sep 21, 2009 @ 18:41
    Lee Kelleher
    0

    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

  • Richard Soeteman 4049 posts 12922 karma points MVP 2x
    Sep 22, 2009 @ 11:56
    Richard Soeteman
    1

    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

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Sep 22, 2009 @ 12:39
    Lee Kelleher
    0

    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

  • Richard Soeteman 4049 posts 12922 karma points MVP 2x
    Sep 22, 2009 @ 12:53
    Richard Soeteman
    0

    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

     

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Sep 22, 2009 @ 13:24
    Ismail Mayat
    0

    Lee,

    This any good someone else having same issue although in datatype.

    Regards

    Ismail

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Sep 22, 2009 @ 14:41
    Aaron Powell
    1

    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

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Sep 22, 2009 @ 15:18
    Simon Dingley
    0

    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.

     

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Sep 22, 2009 @ 15:49
    Lee Kelleher
    1

    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.

    protected void Page_Init(object sender, EventArgs e)
    {
        // populate the drop-down boxes
        this.populateDropDowns();
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            // populate the drop-down boxes
            // this.populateDropDowns(); // moved to Page_Init
        }
    }
    
    private void populateDropDowns()
    {
        // populate the members list
        Member[] members = Member.GetAll;
        if ((members != null) && (members.Length > 0))
        {
            // this.ddlMembers.Items.Clear();
            this.ddlMembers.Items.Add(new ListItem("Select a member...", "-1"));
    
            foreach (Member member in members)
            {
                this.ddlMembers.Items.Add(new ListItem(member.Text, member.Id.ToString()));
            }
        }
    }
    

    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

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Sep 23, 2009 @ 00:33
    Aaron Powell
    1

    Psst - you really should use the ASP.NET Membership API to get the list of members ;)

  • 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