7

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...

zhe
  • 2,358
  • 2
  • 15
  • 12
  • 1
    What are you trying to achieve? Can't you just set the property in the code-behind? (e.g MyControl1.MyCollection = new List { "Foo", "Bar" };) or, " runat="server"/> – RPM1984 Jun 11 '10 at 00:24
  • Of cource, I can set the property in code-behind, as the "emeregency option". What I want, is to separate the UI declaration from the logic. Regarding your second option ("<%= new List..."). It doesn't work, because server tags cannot contain "<% %>" expressions. – zhe Jun 11 '10 at 08:49

5 Answers5

6

You can actually use TypeConverterAttribute to specify a converter class (StringArrayConverter in this case).

stain
  • 61
  • 1
  • 2
1

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
  • 1,482
  • 14
  • 18
1

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 Medlin
  • 11,857
  • 5
  • 62
  • 68
0

Custom User Control and Friendly Property Item Collection (Like ListBox and ListItems, but with List<Class>) on .ASPX

you can use a Collection and also List.

be sure to check http://msdn.microsoft.com/en-us/library/9txe1d4x%28v=vs.100%29.aspx - it applies very good to user control and on the stackoverflow link above i explained how you can link in the aspx page or webconfig

Community
  • 1
  • 1
JJschk
  • 431
  • 3
  • 8
0

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
  • 1,344
  • 10
  • 16
  • 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 Jul 16 '10 at 08:45