开发者

ASP.NET UserControl Uri property

开发者 https://www.devze.com 2023-01-03 21:07 出处:网络
I want to pass a property of the type System.Uri to an WebControl from inside an aspx page. Is it possible to pass the property like that:

I want to pass a property of the type System.Uri to an WebControl from inside an aspx page.

Is it possible to pass the property like that:

<MyUserControl id="myusercontrol" runat="server">
    <MyUrlProperty>
        &l开发者_高级运维t;System.Uri>http://myurl.com/</System.Uri>
    </MyUrlProperty>
</MyUserControl>

instead of:

<MyUserControl id="myusercontrol" runat="server" MyUrlProperty="http://myurl.com/" />

which can't be casted from System.String to System.Uri

EDIT

The control is a sealed class and I don't want to modify it or write an own control. The goal is to set the url-property which is of the type System.Uri and not System.String.


To answer your actual question: no, you can't change the way you pass in the value of a property, like your example shows, without changing the code behind how that property is defined. Now on to your actual issue...

I didn't have any problem passing a string into a property of type URI on my user control and having it be auto converted from string to uri. Are you sure that the Uri you are passing in is valid? If the string you are passing in, like in a databinding scenario, wasn't properly formatted I could see this issue arising maybe.

Sample Code I used to test:

<uc1:WebUserControl ID="WebUserControl1" runat="server" MyUrlProperty="http://www.example.com" />

Code Behind:

Partial Class WebUserControl
    Inherits System.Web.UI.UserControl

    Public Property MyUrlProperty() As Uri
        Get
            Dim o As Object = ViewState("m")
            If o IsNot Nothing Then
                Return DirectCast(o, Uri)
            Else
                Return Nothing
            End If
        End Get
        Set(ByVal value As Uri)
            ViewState("m") = value
        End Set
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.Write(MyUrlProperty)
    End Sub
End Class

--Peter

0

精彩评论

暂无评论...
验证码 换一张
取 消