2010-06-16 3 views
0

나는이 같은 Web.config의 사용 defaultValues을 추가 할 수 있습니다 알고사용자 지정 ASP.NET 프로필 속성에 기본 값을 추가하는 방법

<profile> 
    <properties> 
     <add name="AreCool" type="System.Boolean" defaultValue="False" /> 
    </properties> 
</profile> 

을하지만 난 클래스에서 상속 된 프로필이 있습니다

<profile inherits="CustomProfile" defaultProvider="CustomProfileProvider" enabled="true"> 
    <providers> 
    <clear /> 
    <add name="CustomProfileProvider" type="CustomProfileProvider" /> 
    </providers> 
</profile> 

을 Heres 클래스 : 나는 속성의 기본 값을 설정하는 방법을 모르는

Public Class CustomProfile 
    Inherits ProfileBase 

    Public Property AreCool() As Boolean 
     Get 
      Return Me.GetPropertyValue("AreCool") 
     End Get 
     Set(ByVal value As Boolean) 
      Me.SetPropertyValue("AreCool", value) 
     End Set 
    End Property 

End Class 

. 기본 값이 없으면 부울로 변환 할 수없는 빈 문자열을 사용하므로 오류가 발생합니다. 나는 <DefaultSettingValue("False")> _을 추가하려고 시도했지만 그 차이는 없었습니다.

저는 맞춤 프로필 제공자 (CustomProfileProvider)도 사용하고 있습니다.

답변

0

그냥 생각, 당신이 또는 일부 대신 .length를 사용 dbnull.value() 또는 당신이 실제 항목 인 경우 확인해야하지만?

에 코드를 편집의 의미 변화 (뭔가를 할 수 있습니다

+0

난 오류가 설정 부분을 업데이트 부울 매개 변수 –

+0

에 대한 값으로 설정 ","오류에서 오는 것 같아요. 속성 설정에서 수동 무시를해야하는 경우 여기에서 논리 검사를 수행 할 수도 있습니다. 여기에서 문자열 길이 대신 boolean.tryParse를 사용합니다. – Tommy

+0

공백 문자열을 값 매개 변수의 부울로 변환 할 때 오류가 발생하므로 Set 함수를 입력하지 않습니다. –

0

typic 최종 클래스 빈 세트 매개 변수

에게

공공 클래스 CustomProfile 상속 ProfileBase

Dim _outBool as boolean 
Public Property AreCool() As Boolean 
    Get 
     Return Me.GetPropertyValue("AreCool") 
    End Get 
    Set(ByVal value As Object) 
     ''if the value can be parsed to boolean, set AreCool to value, else default to false'' 
     If([Boolean].TryParse(value, outBool) Then 
      Me.SetPropertyValue("AreCool", value) 
     Else 
      Me.SetPropertyValue("AreCool", False) 
     End If 

    End Set 
End Property 

처리 Microsoft가 .NET Framework에서 수행하는 일반적인 방법은 get 부분을 사용하여 값을 변환 할 수 있는지 확인하고 그렇지 않은 경우 기본값을 반환하는 것입니다. 예를 들어 :

Public Class CustomProfile 
    Inherits ProfileBase 

    Public Property AreCool() As Boolean 
     Get 
      Dim o as Object = Me.GetPropertyValue("AreCool") 
      If TypeOf o Is Boolean Then 
       Return CBool(o) 
      End If 
      Return False 'Return the default 
     End Get 
     Set(ByVal value As Boolean) 
      Me.SetPropertyValue("AreCool", value) 
     End Set 
    End Property 

End Class