简体   繁体   English

如何初始化复杂类型(struct \\类)的自定义控件属性(属性)

[英]How to initialize Custom control attribute (property) of complex type (struct\ class)

I have a custom control in asp.net My custom control have a class member of type Size (value type with Width and Height inner members). 我在asp.net中有一个自定义控件,我的自定义控件具有一个类型为Size(具有Width和Height内部成员的值类型)的类成员。 I would like to initialize this member from the .aspx file. 我想从.aspx文件初始化此成员。

The ideal solution will be (this line will not pass compilation): 理想的解决方案是(此行将不通过编译):

<CustomControl:MyCtrl runat="server" ID="MyCtrlID" MaxSize="{Width=200, Height=400}"/>

The code at the .cs file: .cs文件中的代码:

public partial class MyCtrl: System.Web.UI.UserControl
{
    public System.Drawing.Size MaxSize { get; set;}

    // Class logic...
}

Of curse I can solve this issue by adding logic to the setter (at the c# code), like this: 当然,我可以通过在setter中添加逻辑来解决此问题(使用c#代码),如下所示:

private System.Drawing.Size m_MaxSize;
public string MaxSize 
{ 
    set 
    { 
        string[] sizes = value.Split(",");
        m_MaxSize.Width = sizes[0];
        m_MaxSize.Height = sizes[1];
    }
}

But do we have any asp.net syntax to do this for us?, any help is greatly appreciated. 但是,我们是否有任何asp.net语法可以为我们执行此操作?非常感谢您的帮助。

Since Size Structure uses SizeConverter Class to convert and access Size properties, the following example demonstrates how to properly delcare MaxSize property: 由于Size Structure使用SizeConverter类来转换和访问Size属性,因此下面的示例演示如何正确地delcare MaxSize属性:

<CustomControl:MyCtrl runat="server" ID="MyCtrlID" MaxSize="200, 400"/>

About Type Converters 关于类型转换器

Type Converters 类型转换器

provides a unified way of converting types of values to other types, as well as for accessing standard values and subproperties. 提供了将值的类型转换为其他类型以及访问标准值和子属性的统一方法。

ASP.NET uses type converters at run time to serialize and deserialize objects stored in control state and in view state, follow Type Converter Example for a more details. ASP.NET在运行时使用类型转换器对存储在控件状态和视图状态中的对象进行序列化和反序列化,有关更多详细信息,请参见类型转换器示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM