【问题标题】:Sending an array as a server tag's property将数组作为服务器标签的属性发送
【发布时间】:2010-12-15 05:03:36
【问题描述】:

我想知道是否可以将字符串数组发送到标签的属性

<SampleTag:Form 
  runat="server"  
  ID="sampleform1"
  Items={item1,item2,item3,item4}
>
</SampleTag:Form>

这不起作用,因为它将“{item1,item2,item3,item4}”作为字符串发送到类。

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    最好在后面的代码中这样做

    <SampleTag:Form runat="server" ID="sampleform1"></SampleTag:Form>
    
    sampleform1.Items = new { item1, item2, item3, item4 };
    

    【讨论】:

      【解决方案2】:

      您可能需要向您的属性添加属性,但您应该能够继续使用 xml 端来执行分配:

      <SampleTag:Form
        runat="server"
        ID="sampleform1">
      
          <Items ID="item1">item1</Items>
          <Items ID="item2">item2</Items>
          <Items ID="item3">item3</Items>
          <Items ID="item4">item4</Items>
      
      </SampleTag>
      

      这篇文章可能会提供一些额外的见解:http://msdn.microsoft.com/en-us/library/aa478964.aspx

      【讨论】:

        【解决方案3】:

        我知道您已经接受了答案,但我想让您知道使用类型转换器可以完成您想做的事情。我更喜欢这种方法,因为它使控件对设计者更友好,也更易于其他人使用。

        使用:(对不起VB代码...)

        <cc1:ServerControl1 ID="ServerControl11" runat="server" Items="Testing,Test2,Test3,Test4,Test5" />
        

        代码:

        关键是在属性上使用TypeConverter属性(定义见该类后面)。

        Public Class ServerControl1
            Inherits WebControl
        
            <TypeConverter(GetType(StringToArray))> _
            Public Property Items() As String()
                Get
                    If ViewState("Items") IsNot Nothing Then
                        Return ViewState("Items")
                    Else
                        Return New String() {}
                    End If
                End Get
                Set(ByVal value As String())
                    ViewState("Items") = value
                End Set
            End Property
        
            Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
                MyBase.Render(writer)
        
                For Each s As String In Items
                    writer.Write(String.Format("-{0}<br/>", s))
                Next
            End Sub
        End Class
        
        Public Class StringToArray
            Inherits TypeConverter
        
            Public Overloads Overrides Function CanConvertFrom(ByVal context As ITypeDescriptorContext, ByVal sourceType As Type) As Boolean
                If sourceType Is GetType(String) Then
                    Return True
                End If
                Return MyBase.CanConvertFrom(context, sourceType)
            End Function
        
            Public Overloads Overrides Function ConvertFrom(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object) As Object
                If TypeOf value Is String Then
                    Dim v As String() = CStr(value).Split(New Char() {","c})
                    Return v
                End If
                Return MyBase.ConvertFrom(context, culture, value)
            End Function
        End Class
        

        【讨论】:

          猜你喜欢
          • 2016-06-26
          • 1970-01-01
          • 2012-11-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-03-12
          相关资源
          最近更新 更多