views:

217

answers:

3

For a large fillin form I use the asp.net FormView for the magic databinding to my model. In this model I've a property called Rides (shown below), which exposes a list, which I obviously not want to be replaced entirely. So I made it readonly.

However, this way I can't use the databinding features anymore. Is there a common solution for this problem?

public IList<Ride> Rides
{
    get
    {
        if (this._rides == null)
        {
            this._rides = new List<Ride>();
        }

        return this._rides;
    }
}
+1  A: 

Hi Monty, You can still use the databinding, instead of <%# Bind("PropertyName")%> use <%# Eval("PropertyName")%>, as Bind is usually for 2 way binding & Eval is usually for evaluation of the underlying datasources, Bind will be handy when you have a setter as well. Hope this sheds enough light & you will find it helpful.

StevenzNPaul
Thanks for the answer. However, it's my purpose to be able to use the property rides also in the FormView. And preferably I'd like the property to be databound (<%# Bind("PropertyName")%> instead of <%# Eval("PropertyName")%>).
Monty
A: 

Wild stab in the dark, don't know if this will work but I guess you can try...

How about putting the setter (set) back in the property, but make it assign to itself?

eg.

set
{
    this._rides = this._rides;  // Rather than this._rides = value;
}

This would mean the value is untouchable from the property, if a set operation is attempted it won't do any damage and should still bind...

Jamie Chapman
Thanks for the reply, however it won't work... This way the DataBind() method still can't bind the value back to its source.
Monty
A: 

Monty, Take a look at a class named BindingList. Binding list enables two-way binding. You can create it from Yor collection in code and bidn it to the datasource property of the FormView. I think this is what You want. Also by exposing IList YOU have not made this thing read-only. I can recast to the List and I can modify You items all the way. If You really want to expose rides as read-only return IEnumerable and return not a List by a ReadOnlyCollection... recasting list to the other class wont' help.

luckyluke
I guess this will do, as close it can get I think. Of course I know exposing as IList won't make it read-only. Will have a try on this, thanks.
Monty
Sorry, I see that You meant, that the property is a get-only. My bad:)
luckyluke