65.9K
CodeProject 正在变化。 阅读更多。
Home

获取任何控件的任意类型的值

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (1投票)

2009年7月6日

CPOL

1分钟阅读

viewsIcon

13608

获取指定类型中任何控件的值。

引言

在获取或设置任何控件的“值”属性时,该值都是 String 类型。当我们需要将该值转换为任何其他类型时,必须检查该值是否为空,并根据该值进行代码逻辑处理。 同样,在将值设置为控件时,需要将该值转换为 String。 对于任何值类型(nullable 类型除外)来说,这都可以接受。 对于其他所有类型,我们可以使用 ToString(),这就可以了,但对于 nullable 类型,我们必须调用该类型的 HasValue 属性。

为了以一种更复杂的方式克服这个难题,我们将使用一些泛型方法,这些方法可以将控件返回的 String 值转换为任何类型,并将任何类型的值转换为 String 以设置为控件。

使用代码

为了演示,我们将使用一个名为 Address 的泛型对象。 对象本身的详细描述在这里并不重要,但我们必须知道该对象具有以下属性:AddressID (Integer)、Address1 (String)、StateID (Integer) 和 CountryID (Nullable(of Integer)),并且该对象可以使用 AddressID 从数据库加载一个 Address 记录。

我创建了三个方法

  • GetControlValue(以指定类型返回控件的值)。
  • ConvertFromString(如果可能,将 String 值转换为任何类型)。
  • ConvertToString(如果可能,将任何类型的值转换为 String 类型)。

方法定义如下

''' <summary>
''' Returns a Control's value in the specified Type.
''' </summary>
''' <typeparam name="T">Return value type.</typeparam>
''' <param name="cntrl"><see 
'''      cref="Control">Control</see>
'''     to get the value from</param>
''' <returns>Value as T</returns>
''' <remarks>
''' <para>Returns Text property value for <see 
'''    cref="TextBox">Textbox</see> and 
'''    <see cref="Label">Label</see></para>
''' <para>Returns Value property value for
'''    <see cref="HiddenField">HiddenField</see></para>
''' <para>Returns SelectedValue property value for
'''    <see cref="DropdownList">DropdownLis</see>
'''    and <see cref="RadiobuttonList">RadiobuttonList</see>.
'''    If default Item is selected returns NULL</para>
''' <para>Returns Checked property value for
'''   <see cref="CheckBox">CheckBox</see></para>
''' </remarks>

Public Shared Function GetControlValue(Of T)(ByVal cntrl As Control) As T
  Dim value As Object = Nothing
    Select Case True
        Case TypeOf cntrl Is TextBox
            value = CType(cntrl, TextBox).Text.Trim
        Case TypeOf cntrl Is HiddenField
            value = CType(cntrl, HiddenField).Value
        Case TypeOf cntrl Is DropDownList
            value = CType(cntrl, DropDownList).SelectedValue
        Case TypeOf cntrl Is RadioButtonList
            value = CType(cntrl, RadioButtonList).SelectedValue
        Case TypeOf cntrl Is CheckBox
            value = CType(cntrl, CheckBox).Checked
        Case TypeOf cntrl Is Label
            value = CType(cntrl, Label).Text
    End Select
    Return ConvertFromString(Of T)(value.ToString())
End Function

''' <summary>
''' Converts a string value to the specified type
''' </summary>
''' <typeparam name="T">Type to convert to</typeparam>
''' <param name="value">String value to convert</param>
''' <returns>Value as T</returns>
''' <remarks>
''' <para>If control value is not an empty string and
''' the if the value can be converted to the specified return type, 
''' Converts and returns the value as the specified type.</para>
''' <para>If control value is an empty string, returns Nothing</para>
''' </remarks>
Public Shared Function ConvertFromString(Of T)(ByVal value As String) As T
 Try
  If Not String.IsNullOrEmpty(value) And _
     TypeDescriptor.GetConverter(GetType(T)).IsValid(value) Then
   Return CType(TypeDescriptor.GetConverter(_
                GetType(T)).ConvertFromString(value), T)
  End If
 Catch ex As Exception

 End Try

 Return Nothing
End Function

''' <summary>
''' Converts and Returns <see cref="String">String</see>
''' representation of a value of any Type.
''' </summary>
''' <typeparam name="T">Value Type</typeparam>
''' <param name="value">Value to convert</param>
''' <returns>Value as <see cref="String">String</see></returns>
Public Shared Function ConvertToString(Of T)(ByVal value As T) As String
  Try
    If TypeDescriptor.GetConverter(GetType(T)).CanConvertTo(GetType(String)) Then
       Return TypeDescriptor.GetConverter(GetType(T)).ConvertToString(value).Trim
    End If
  Catch ex As Exception

  End Try
  Return Nothing
End Function

获取控件值

dim address as New Address
address.Address1 = GetControlValue(of String)(me.txtAddress1)
address.StateID = GetControlValue(of Integer)(me.ddlStates)
address.CountryID = GetControlValue(of _
   Nullable(of Integer))(me.ddlCountries)

设置控件值

dim address as new Address
address.AddressID = 10
address.Load()
me.txtAddress1.Text = ConvertToString(of String)(address.Address1)
me.ddlStates.SelectedValue = ConvertToString(of Integer)(address.StateID)
me.ddlCountries.SelectedValue = _
  ConvertToString(of Nullable(of Integer))(address.CountryID)

历史

没有更新。

© . All rights reserved.