Hello wonderful people of Code Project!
So here's my situation:
I have a custom textbox web control I've made that is designed to accept user input of a percentage and store them in its decimal equivalence (e.g. "13.5" => "0.135"). Simillarly, when the textbox is bound it will take the decimal value from the datasource and display it as its percentage equivalence (e.g. "0.135" => "13.5").
<DefaultProperty("Text"),
ToolboxData("<{0}:PercentageTextBox runat="server"></{0}:PercentageTextBox>")> _
Public Class PercentageTextBox
Inherits TextBox
Implements IValidator
#Region "Properties"
Public Property Editable As Boolean
Private ReadOnly Property InputFormatType As PercentageType
Get
Return PercentageType.InPercent
End Get
End Property
<Bindable(True),
Category("Appearance"),
DefaultValue(""),
Description("The name of the percentage"),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
PersistenceMode(PersistenceMode.InnerProperty)>
Public Overridable Property Name As String
<Bindable(True),
Category("Appearance"),
DefaultValue(""),
Localizable(True)>
Overrides Property Text() As String
Get
Dim s As String = CStr(ViewState("Text"))
If s Is Nothing Then
Return String.Empty
Else
Return s
End If
End Get
Set(ByVal Value As String)
Dim output As String = Value
Try
Dim TextVal = CDec(Value)
Dim converted As New PercentageValue(TextVal, _
PercentageType.InDecimal)
output = CStr(converted.Value)
Catch ex As Exception
output = Value
Finally
ViewState("Text") = output
End Try
End Set
End Property
#End Region
Protected Overrides Sub Render(writer As HtmlTextWriter)
Try
If Editable Then
Dim val = CDec(ViewState("Text"))
val = System.Math.Round(val * 10000, 2)
ViewState("Text") = val
End If
Catch ex As Exception
Finally
MyBase.Render(writer)
End Try
End Sub
End Class
N.B. I stripped out the validation code added to this control; also, you don't see the math in the control itself for converting the input as it uses the class
PercentageValue
to convert the value based upon the enumerator
PercentageType
. The idea behind this was that this could be interacted with to switch method based on the
InputFormatType
property in the future.
Now this is my first custom control I've made so I'm willing to try and improve how this works:
Firstly: I am not sure about where my conversion takes place in the control. Should I be doing the input conversion in the
Text()
method and the inverse in the
Render()
method. Reason I say this is that when the control is editing a stored value the
Set
property is called unnecessarily (resulting in my conversion in
Render()
having to x10000 to undo the previous set call).
Secondly: I've creating the property
Editable
which needs to be set manually when I use the control, such as in an EditItemTemplate in a DetailsView. This is to distinguish it from a user inputting a new value. Is there some property of the inherited textbox that I can infer this from instead of having to set it as such manually?
Any pointers or advice would be much appreciated.
Thanks,