Search code examples
c#asp.netweb-parts

Correct way to implement web part personalisation for listboxes


Trying to work out this whole web part personalisation, and trying to implement it for a list box.

Well the end result will be two list boxes, with interchangeable values (ie, a value will only exist in one of the listboxes)

But I can't maintain the datasource for it. So maybe I'm going about it wrong?

This is what I have for a test H2 tag on the page

[Personalizable(PersonalizationScope.User)]
public string LabelText {
    get { return h2Test.InnerText; }
    set { h2Test.InnerText = value; }
}

And it works fine, if I have a textbox and use it to change the value of LabelText, then when I close the browser it automagically persists the change.

So I thought, ok, then maybe the same will work with a list box

[Personalizable(PersonalizationScope.User)]
public DomainList Domains {
    get { return (DomainList)lstBxDomains.DataSource; }
    set {
        lstBxDomains.DataSource = value;
        lstBxDomains.DataBind();
    }
}

Where DomainList is just a class which extends List, and Domain is just a three field class, int, string, string.

But it doesn't, so is this too complicated for the webpart personalisation automagican, or have i just implement it wrongly (Which is more than likely)

This is my event handler to remove the items from the list:

protected void btnRemDomain_Click(object sender, EventArgs e) {
    if (IsPostBack && lstBxDomains.SelectedIndex > -1) {
        for (int i = 0; i < lstBxDomains.Items.Count; i++) {
            if (lstBxDomains.Items[i].Selected) {
                Domains.Remove(Domains.Find(d => d.ID.ToString() == lstBxDomains.Items[i].Value));
            }
        }
        Domains = Domains;
    }
}

The Domains=Domains; line is in there to see if explicitly setting the value made a difference (as Removing doesn't acutally reset the value of the field), but it doesn't. I've also tried creating a new local DomainList setting it to the global one, and then doing the remove/find on it, and then setting the local one to the global. But not working either.


Solution

  • I have managed to resolve this by using WebPart.SetPersonalizationDirty(this); in the set accessor of Domains, but would someone mind confirming if this is an appropriate way to do it?