Anything similar to WPF's ValueConverters for ASP.Net?
-
12-09-2019 - |
Question
After doing a project with WPF and getting very much attached to it's excellent databinding capabilities, I have now moved back to ASP.Net and am starting to miss some of WPF's stuff.
My question is...is there anything similar to WPF's ValueConverters for ASP.Net?
For example, I want a UserControl to expose a public property which is an IEnumerable<int>
and the user can enter such from the source:
<asp:MyControl1 runat="server" ID="something" TheList="1,3,5,7"/>
And then the value in TheList
will be converted to an IEnumerable<int>
, with something like a ValueConverter of WPF's.
[Update] Solution
Thanks to Nathan Baulch's mention of a TypeConverter
, I got across this question and I managed to build a string to list converter.
Solution
I would recommend you take a look at System.ComponentModel.TypeConverter.
This article on CodeProject includes an example that is quite similar to yours involving latitude and longitude strings.
OTHER TIPS
I think not. My best guess is implicit conversion from String to IEnumberable, which in general is a very bad thing, but would work in your case.
Everything in the markup is always treated as String, unless put in <% %>
- then it is being interpreted.
What else you could do is to define IEnumerable DataSource control. Then MyControl would have TheListDataSourceID from where it will take the IEnumerable data.
<asp:MyControl1 runat="server" ID="something" TheListDataSourceID="myDataSource" />
<asp:MyIEnumerableDataSource runat="server" ID="myDataSource" TheList="1,3,5,7" />
Then the IEnumerableDataSource would do the conversion and would return IEnumerable for the user control wanting it. Much better case than the implicit conversion.