views:

336

answers:

3

Hello. I created user control. It has string[] public property (it may be List<string> or whatever). I want to support defining this property in aspx code, when declaring the instance of this usercontrol. Something like this:

<uc1:MyControl ID="MyControl1" runat="server">
    <MyStringCollectionProperty>
        <string>My String 1</string>
        <string>My String 2</string>
        <string>My String 3</string>
    </MyStringCollectionProperty>
</uc1:MyControl>

How to make it work? Thanks...

A: 

By entering your variable values in the mark-up explicitly, per your example, you are directly combining your UI with your logic.

If these values don't ever change, then there is no point to putting them in the mark-up -- it would be better to put them into your user-control's code.

If these values could be different each time the user control loads, then they ought to come from a dynamic source, such as session state variables, public variables, control values, etc. (In that case, your user control would look them up each time it loads.)

If these values change only at design time, you could still set them onload as above, but you might consider looking at building a 'custom control' in that case.

Anyway, not sure exactly what you're trying to achieve, so hope one of these options can help.

Good luck

dave
Actually this property is not tightly connected with the business logic. It just represents one of the settings of this user control.However, setting this property in page_load now satisfies me. :)Thanks for your advice.
zhe
A: 

As far as I know you cannot do what you want to do with a user control. You would need to write a custom control.

See Server Control Properties Example on MSDN.

Johann Strydom
A: 

Here is an example of how to accomplish what you are asking for. Child controls are read in as HTMLGeneric Controls, so you have to convert them. As they are read in, I convert them into my custom "StringControl" class. If you want to use a more advanced structure you can just enhance the constructor for the stringcontrol.

Partial Class UserControlStrings_MyControl
    Inherits System.Web.UI.UserControl

    Private _MyStringCollectionProperty As New MyUserControlStringCollectionClass(Me)

    <PersistenceMode(PersistenceMode.InnerProperty)>
    Public ReadOnly Property MyStringCollectionProperty As MyUserControlStringCollectionClass
        Get
            Return _MyStringCollectionProperty
        End Get
    End Property
End Class

Public Class MyUserControlStringCollectionClass
    Inherits ControlCollection

    Sub New(ByVal owner As Control)
        MyBase.New(owner)
    End Sub

    Public Overrides Sub Add(ByVal child As System.Web.UI.Control)
        MyBase.Add(New StringControl(child))
    End Sub
End Class

Public Class StringControl
    Inherits HtmlGenericControl

    Sub New(ByVal GenericControl As HtmlGenericControl)
        MyBase.New()
        Me.Value = GenericControl.InnerHtml
    End Sub

    Public Property Value As String = String.Empty

    Public Overrides Function ToString() As String
        Return Value
    End Function
End Class

You can then look in the "MyControl1.MyStringCollectionProperty" for all the values.

Carter