views:

21

answers:

1

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>
        <System.Uri>http://myurl.com/&lt;/System.Uri&gt;
    </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.

+1  A: 

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

Patricker
The problem is that the control is a sealed class and I can't modify it. I just want to use this control and set the given property of the type System.Uri. So writing an own control is not an option.
wroks
@Roedel: Changed my answer, see if that helps any more then the last one.
Patricker
Hi Patricker. That's interesting, I'll have to check the value passed to the property. If the convert works for you it has to be on my side. Maybe there's an invalid sign in it or something. I'll tell you as soon as I checked it.
wroks
Found out that it was a bad url so the cast could not be made. Thank's for putting me on the right track.
wroks