I'm working on a control in which I want to add databindings based on the values set in other properties. I thought I could accomplish this using the IDataBindingsAccessor interface. However, when adding DataBindings through the controls IDataBindingsAccessor
implementation the bindings are not written out in the markup of the .aspx page which is the effect I want. I found I was able to do this using the control designer's obsolete OnBindingsCollectionChanged method. This method however uses the Tag property of
the control designer to add the binding. The actual control I'm working on contains a collection of the controls that need to be able to add bindings and it seems because it's a collection property of another control it can't access it's designer's Tag property.
Does anyone have any suggestions on how to do this?
Let me expand a little bit on what I'm doing here. I just made the switch from 1.1 to 2.0 and I've noticed a few things that could be improved while trying to use ObjectDataSources and two-way binding and everything. For instance one problem I face is that
if you have a dropdownlist inside a formview and the dropdownlist's selected value (or any other property for that matter) is bound to the dataitem from the formview, but is also bound to it's own datasource. If your cache expires on that dropdownlist or the
dropdownlist needs to be rebound for any other reason (perhaps it's based on a previous dropdownlist) then you will get an error during binding because it will try to bind to both it's datasource and it's container's datasource. Now the workaround I've seen
most commonly for this is to put a label in the formview and have that two-way bound, then set the selected value based on the label during binding and set the label to the selectedvalue when the selectedvalue changes. This works great and is in essence what
I'm trying to achieve. I'm creating a control that acts as a separate layer inbetween the binding and the control itself so DataBoundControls don't have the problem of being bound to two DataSources (ideally there should be a way to bind separately or only
the source that's available should be bound or even call the EnsureDataBound() of the containing control).
Anyway, with all that said what I'm doing here is I have a control (Binding) it has a ControlID and PropertyName. It also has a Binding property which is of type object which is the property that's getting bound. It's get and set methods simply use reflection
to get references to the get and set methods of PropertyName on the control specified by ControlID. So in binding to the Binding property you are really binding to PropertyName on ControlID. I have an Expression property which is basically what I want to use
to set the binding. So when everything is configured it will look something like this:
Another nice feature about this added layer between bindings and the control itself is it lets me handle binding errors on a per instance basis. Often times if a value being bound doesn't exist in a dropdownlist I don't want to throw an exception. I just want
it to clear the dropdownlist or take some other action.
If you have any suggestions on better ways for doing anything I'm trying to do please let me know. I'm still learning all the ins and outs of 2.0 and I'm open to suggestions.
amn3sia
Member
115 Points
23 Posts
IDataBindingsAccessor Question
Mar 20, 2006 10:19 PM|LINK
I'm working on a control in which I want to add databindings based on the values set in other properties. I thought I could accomplish this using the IDataBindingsAccessor interface. However, when adding DataBindings through the controls IDataBindingsAccessor implementation the bindings are not written out in the markup of the .aspx page which is the effect I want. I found I was able to do this using the control designer's obsolete OnBindingsCollectionChanged method. This method however uses the Tag property of the control designer to add the binding. The actual control I'm working on contains a collection of the controls that need to be able to add bindings and it seems because it's a collection property of another control it can't access it's designer's Tag property. Does anyone have any suggestions on how to do this?
amn3sia
Member
115 Points
23 Posts
Re: IDataBindingsAccessor Question
Mar 21, 2006 01:29 AM|LINK
Anyway, with all that said what I'm doing here is I have a control (Binding) it has a ControlID and PropertyName. It also has a Binding property which is of type object which is the property that's getting bound. It's get and set methods simply use reflection to get references to the get and set methods of PropertyName on the control specified by ControlID. So in binding to the Binding property you are really binding to PropertyName on ControlID. I have an Expression property which is basically what I want to use to set the binding. So when everything is configured it will look something like this:
<cc:DataBindingHelper runat="server" ID="DataBindingHelper1">
<cc:Binding ControlID="ddlCategory" PropertyName="SelectedValue" Binding='<%# Bind("CategoryID") %>' />
<cc:Binding ControlID="ddlSubcategory" PropertyName="SelectedValue" Binding='<%# Bind("SubcategoryID") %>' />
</cc:DataBindingHelper>
Another nice feature about this added layer between bindings and the control itself is it lets me handle binding errors on a per instance basis. Often times if a value being bound doesn't exist in a dropdownlist I don't want to throw an exception. I just want it to clear the dropdownlist or take some other action.
If you have any suggestions on better ways for doing anything I'm trying to do please let me know. I'm still learning all the ins and outs of 2.0 and I'm open to suggestions.