.net custom control with databinding to a property

Recently, I’ve been writing a custom control that extends a command button.

The premise is quite simple, the control cycles through a list of user provided colours, changing the background colour on each click to the next one on the list, wrapping around to the beginning when it reached the end.
(the CMS I was developing for a client required a series of flags that could be set to alert the user to details about the person calling [eg, problem client, unpaid bill, priority client etc])

As I wanted to save the value of the current I wanted to make the button data bindable. Each time the button is clicked the value of a property is incremented to represent the current flag state, this value would be databound.
Unfortunately, although the value updated internally, it wouldn’t push the changes through the databinder automagically for me. The state index would be set on load, changing the colour to the required value, meaning that the databinding is indeed working, but that there was nothing causing the binding manager to write the changes back the other way.

After a little reading, and some autocomplete-fu I got my solution, 1 line of code forcing the databinding to write its changes.

Public Property StateIndex As Int32
Get
  Return iStateIndex
End Get 
Set(ByVal value As Int32) 
'relevant code
  Me.DataBindings.Item("StateIndex").WriteValue() 
'
End Set 
End Property

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">