title: How to get value from System.Web.UI.Control (base class)?
body: <p>Is there a way to access a control's value/text using the base class? </p>
  
  <p>Looking at <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox%28v=vs.100%29.aspx" rel="nofollow">TextBox</a> and <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hiddenfield%28v=vs.100%29.aspx" rel="nofollow">HiddenField</a>, System.Web.UI.Control is the most specific base class they share. Seems like a good candidate, but I don't see an obvious way to access the text/value.</p>
  
  <p>I do see that both class definitions use the <em>ControlValuePropertyAttribute</em> to identify the property that holds their text/value... e.g. HiddenField is set to "Value", TextBox is set to "Text". Not sure how to use that info to retrieve the data, though.</p>
  
  <p><strong>Background</strong></p>
  
  <p>I have an extension method that takes the text of some web control (e.g., a TextBox) and converts it to a given type:</p>
  
  <pre class="lang-vb prettyprint-override"><code>&lt;Extension()&gt; _
  Public Function GetTextValue(Of resultType)(ByVal textControl As ITextControl, ByVal defaultValue As resultType) As resultType
      Return ConvertValue(Of resultType)(textControl .Text, defaultValue)
  End Function
  </code></pre>
  
  <p>Now, I need the value of a HiddenField (which does not implement the ITextControl interface). It would be easy enough to overload the function, but it would be awesome to just handle the base class and not have to deal with writing overloads anymore.</p>
  
  <p><strong>Edit - additional info</strong></p>
  
  <p>Here is an example of how the extension method is used</p>
  
  <pre class="lang-vb prettyprint-override"><code>Dim myThing as Decimal = tbSomeWebControl.GetTextValue(Of Decimal)(0)  ' Converts the textbox data to a decimal
  Dim yourThang as Date = hdnSomeSecret.GetTextValue(Of Date)(Date.MinValue)  ' Converts the hiddenfield data to a Date
  </code></pre>
  
  <p>Currently, this requires two overloads because data is accessed using the Value property in hiddenfields and the Text property in textboxes.</p>
  
  <p>What's wrong with overloads? Nothing, except i'm writing nearly the same code over and over (just modifying the 1st parameter of the definition and the 1st argument of its call).</p>