Copied to clipboard

Flag this post as spam?

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


  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Sep 26, 2011 @ 11:38
    Bo Damgaard Mortensen
    0

    Save checkboxlist values from usercontrolwrapper

    Hi all,

    I'm having some trouble saving values from a CheckboxList asp.net control using the usercontrolwrapper.

    My usercontrol reads data from an external XML file and publishes it as a checkboxlist so the Umbraco user is able to select the data he/she wants to display on the site.

    So far so good: the checkboxlist displays correctly in Umbraco, but I simply can't get it to save the selected values (which are unique IDs from the XML file) as a comma separated string. What I have so far is:

    protected void Page_Load(object sender, EventArgs e)
            {
                GetData();
            }
    
            public void GetData()
            {
                string url = "myxmlfeed";
                XmlTextReader reader = new XmlTextReader(url);
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(reader);
                XmlNodeList cars = xmlDoc.GetElementsByTagName("car");
                List<AdminCar> carList = new List<AdminCar>();
                foreach (XmlElement element in cars)
                {
                    carList.Add(new AdminCar()
                    {
                        Id = element.Attributes["number"].Value,
                        Model = element.ChildNodes[1].SelectSingleNode("car_model").InnerText,
                        ModelVarient = element.ChildNodes[1].SelectSingleNode("model_variant").InnerText,
                        Name = element.ChildNodes[1].SelectSingleNode("car_manufacture").InnerText,
                        Year = element.ChildNodes[1].SelectSingleNode("model_year").InnerText,
                        FullDescription = element.Attributes["number"].Value + " - " + element.ChildNodes[1].SelectSingleNode("car_manufacture").InnerText + " " + element.ChildNodes[1].SelectSingleNode("car_model").InnerText
                    });
                }
    
                if (carList.Count > 0)
                {
                    CheckBoxList1.DataSource = carList;
                    CheckBoxList1.DataTextField = "FullDescription";
                    CheckBoxList1.DataValueField = "Id";
                    CheckBoxList1.DataBind();
                }   
            }
    
            public object value
            {
                get
                {
                    string result = string.Empty;
                    if (!Page.IsPostBack)
                    {
    
                        foreach (ListItem item in CheckBoxList1.Items)
                        {
                            if (item.Selected)
                            {
                                result += item.Text + ", ";
                            }
                        }
                    }
                    return result;
                }
                set
                {
                    lblTest.Text = value.ToString();
                }
            }

    Anyone got a clue about what I'm doing wrong here? :-) And as a sidequestion: by saving the IDs as a comma separated string, will I then be able to get these with umbraco.library:GetPreValues() ?

    Thanks a lot in advance!

    All the best,

    Bo

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Sep 26, 2011 @ 12:29
    Bo Damgaard Mortensen
    0

    Ok, I've come to the conclusion that the problem is due to postback issues since the following code actually does get stored when clicking save or save and publish:

    H

    public object value
            {
                get
                {
                    string result = string.Empty;
    
                    foreach (ListItem item in CheckBoxList1.Items)
                    {                    
                        result += item.Text + ", ";                               
                    }
                    return result;
                }
                set
                {
                    lblTest.Text = value.ToString();
                }
            }

    So, now the million-dollar question is: how to retain the selected values from the checkboxlist when Save or Save and Publish is clicked?

Please Sign in or register to post replies

Write your reply to:

Draft