We all know XMod Pro is an excellent tool for linking forms to your database. It does this with data-binding. Each control can be bound to a column in your data source simply by specifying that column name as the control’s DataField. But, what about making your controls more dynamic? How can you adjust other properties in your control based on values in the database?
One of our community members, Stefan, posed this question on the forums:
I saw an earlier post about creating a custom server control that implements the XMod Pro IDataBoundControl interface to have the control participate in the XMod databinding. My control actually requires that 2 properties are passed in before rendering. If I implement the IDataBoundControl interface I can only have one DataField. Is there a different way I can construct my composite control that would allow for binding of multiple properties through XMod?
Yes – with a caveat. You can bind multiple properties on your control. This applies regardless of whether your control is a custom control or not.
Simply use the token syntax to do so as in the example below (formatting and other tags have been omitted for clarity):
<SelectCommand CommandText ="SELECT '400' AS Width, FirstName FROM Authors" >
<TextBox Id="FirstName" DataField="FirstName" DataType="String" Width='[[Width]]' />
In the example above, the textbox is being bound to the FirstName column as you would normally do. Additionally, we've added the [[Width]] token as the value of the Width property. Note also that the property value is delimited by single quotes - required when using tokens as property values.
You can see that the FirstName textbox (the first one on the form is being bound to the FirstName column – with a value of “Kelly” while the width of the textbox is being set to 400 pixels.
To make the change more pronounced and to show you can bind multiple properties this way, let’s also change the height of the textbox.
<SelectCommand CommandText ="SELECT '400' AS Width, '200 As Height, 'Kelly AS FirstName" >
<TextBox Id="FirstName" DataField="FirstName" DataType="String" MaxLenght="25" Width='[[Width]]' Height='[[Height]]' />
Here’s the result:
Caveat: This method will work for most standard property types. However, because of the order in which the tokens are processed in the life of the form, not all property types can be set this way. For instance, you will receive an error if you try to set the ForeColor property of the TextBox stating that "Value of type 'String' cannot be converted to 'System.Drawing.Color'."
If you are building your own custom control, and run into an error like this, you can set your control's property to accept a string value and then do the conversion internally.