在ASP_NET中的嵌套属性为什么不能实现?(UP有分)

ChinaSunFire 2003-09-15 10:41:00
Imports System.ComponentModel
Imports System.Web
Imports System.Web.UI
Imports System.Drawing.Design


<DefaultProperty("Text"), ToolboxData("<{0}:ScrollBoard runat=server></{0}:ScrollBoard>")> Public Class ScrollBoard
Inherits System.Web.UI.WebControls.WebControl


#Region "变量声明"


Dim _text As String



Private _TitleImage As New ImageInfo()



#End Region



'-------------------------
<TypeConverter(GetType(ImageExpandableObjectConverter))> _
Public Class ImageInfo
Private _imagePath As String = ""
Private _imageWidth As New WebControls.Unit(10)
Private _imageHeight As New WebControls.Unit(10)

Public Property imagePath() As String
Get
Return Me._imagePath
End Get
Set(ByVal Value As String)
Me._imagePath = Value
End Set
End Property

Public Property imageWidth() As WebControls.Unit
Get
Return Me._imageWidth
End Get
Set(ByVal Value As WebControls.Unit)
Me._imageWidth = Value
End Set
End Property

Public Property imageHeight() As WebControls.Unit
Get
Return Me._imageHeight
End Get
Set(ByVal Value As WebControls.Unit)
Me._imageHeight = Value
End Set
End Property

Public Sub New()
'
End Sub
End Class

Public Class ImageExpandableObjectConverter
Inherits ExpandableObjectConverter

Public Overloads Overrides Function CanConvertFrom(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal sourceType As System.Type) As Boolean

If sourceType Is GetType(String) Then
Return True
End If
Return MyBase.CanConvertFrom(context, sourceType)
End Function


Public Overloads Overrides Function CanConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal destinationType As System.Type) As Boolean
If destinationType Is GetType(ImageInfo) Then
Return True
End If
Return MyBase.CanConvertTo(context, destinationType)
End Function


Public Overloads Overrides Function ConvertFrom(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object) As Object
Dim _ImageInfo As New ImageInfo()
If Not value Is Nothing Then
Try

_ImageInfo.imageWidth = New WebControls.Unit(CType(value.ToString.Split(",")(0), Integer))

_ImageInfo.imageHeight = New WebControls.Unit(CType(value.ToString.Split(",")(1), Integer))
_ImageInfo.imagePath = value.ToString .Split(",")(2)
Return _ImageInfo
Catch
MsgBox("Error Converfrom")
End Try
End If
Return MyBase.ConvertFrom(context, culture, value)
End Function

Public Overloads Overrides Function ConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object, ByVal destinationType As System.Type) As Object
If destinationType Is GetType(String) Or value Is GetType(ImageInfo) Then
Dim _ImageInfo As New ImageInfo()
_ImageInfo = CType(value, ImageInfo)
Return _ImageInfo.imagePath.ToString
' Return _ImageInfo.imageWidth.Value.ToString & "," & _ImageInfo.imageHeight.Value.ToString '& "," & _ImageInfo.imagePath.ToString
End If


Return MyBase.ConvertTo(context, culture, value, destinationType)
End Function
End Class

'-------------------------


<Bindable(True), Category("Appearance"), DefaultValue("")> _
Property [Text]() As String
Get
Return _text
End Get

Set(ByVal Value As String)
_text = Value
End Set
End Property

Public Property TitleImage() As ImageInfo
Get
Return _TitleImage
End Get
Set(ByVal Value As ImageInfo)
_TitleImage = Value
End Set
End Property


Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
output.writeLine(me.titleimage.imagepath)
output.WriteLine(me.TitleImage.imageHeight.ToString)
output.WriteLine(me.TitleImage.imageWidth.ToString)
End Sub

End Class

出现两个问题:1、在属性浏览器中是正确的,但在运行时,输出属性的值与设置的值不符。
2、提示服务器错误,在为IMAGEINFO转换属性值时错误!!!


困惑~~~~~

希望大侠们帮忙!!
...全文
33 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
saucer 2003-09-16
  • 打赏
  • 举报
回复

you need a TypeConverter, that is why I suggest you make it a ReadOnly property

Public ReadOnly Property imagePath() As String
Get
Return Me._imagePath
End Get
End Property


it will work fine
ChinaSunFire 2003-09-16
  • 打赏
  • 举报
回复
谢谢saucer(思归) 大侠!!
还有一个小问题需要请教您
在TitleImage类里
<Editor(GetType(System.Web.UI.Design.ImageUrlEditor), GetType(UITypeEditor)), NotifyParentProperty(True)> _
Public Property imagePath() As String
Get
Return Me._imagePath
End Get
Set(ByVal Value As String)
Me._imagePath = Value
End Set
End Property

使用Editor(GetType(System.Web.UI.Design.ImageUrlEditor), GetType(UITypeEditor))将属性同编辑器关联后,为什么在属性浏览器里设置该属性时(用关联的编辑器)提示“未将对象引用设置到对象的实例”。
在嵌套属性里有这个问题。

saucer 2003-09-16
  • 打赏
  • 举报
回复
try

<DesignerSerializationVisibility(DesignerSerializationVisibility.Content), NotifyParentProperty(true)> _
Public Property TitleImage() As ImageInfo
Get
Return _TitleImage
End Get
End Property


...
<NotifyParentProperty(true)> _
Public Property imagePath() As String

<NotifyParentProperty(true)> _
Public Property imageWidth() As WebControls.Unit

<NotifyParentProperty(true)> _
Public Property imageHeight() As WebControls.Unit
ChinaSunFire 2003-09-16
  • 打赏
  • 举报
回复
期待高手出现
wkyjob 2003-09-15
  • 打赏
  • 举报
回复
关注
ChinaSunFire 2003-09-15
  • 打赏
  • 举报
回复
更正:

Public Overloads Overrides Function ConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object, ByVal destinationType As System.Type) As Object
If destinationType Is GetType(String) Or value Is GetType(ImageInfo) Then
Dim _ImageInfo As New ImageInfo()
_ImageInfo = CType(value, ImageInfo)

Return _ImageInfo.imageWidth.Value.ToString & "," & _ImageInfo.imageHeight.Value.ToString & "," & _ImageInfo.imagePath.ToString
End If


Return MyBase.ConvertTo(context, culture, value, destinationType)
End Function

110,532

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧