且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在ASP.NET用户控件露出复杂属性

更新时间:2023-12-06 10:49:58

对不起,回答我的问题,但我已经发现了几个这样做的方式,我想他们可能是有用的人别人也:)

要序列化对象的控制的一个属性,你必须定义一个适当的的TypeConverter ,并应用 TypeConverterAttribute 的属性类型。这里有一个例子:如何:实现类型转换器

更容易,你可以坚持控制的内容,就像这个属性:

 <%@页面语言=C#AutoEventWireup =真codeBehind =Default.aspx.cs继承=wbctrlstst._Default%GT;
<%@注册标签preFIX =X变量名=系统testControlSRC =〜/ TestControl.ascx%GT;
< HTML的xmlns =htt​​p://www.w3.org/1999/xhtml>
<身体GT;
    <表ID =form1的=服务器>
    < D​​IV>
        < X:系统testControl =服务器ID =testlist1>
            &所述; TestProperty x =1y =42/>
        < / X:&系统testControl GT;
    < / DIV>
    < /表及GT;
< /身体GT;
< / HTML>

的唯一要求,有什么我能看到的,是该类型必须有一个默认的构造函数。您可以将 PersistenceModeAttribute 来的属性来指定,物业应在控制的内容是持久的,但它看起来像它的默认行为,这不是严格必要的。

  [PersistenceMode(PersistenceMode.InnerProperty)
公共TESTDATA TestProperty {
    获得{
        返回的ViewState [TestProperty]作为TESTDATA;
    }
    集合{
        的ViewState [TestProperty] =值;
    }
}

I would like to expose a complex property from a custom ASP.NET user control, in such a way that it can be set from the control tag in the aspx page.

Something like this:

public class TestData {
    public int X;
    public int Y;
}

public partial class TestControl : System.Web.UI.UserControl {

    public TestData TestProperty {
        get {
            return ViewState["TestProperty"] as TestData;
        }
        set {
            ViewState["TestProperty"] = value;
        }
    }
}

And then in the .aspx file of a page that contains the control I would like to have something like:

<div>
    <testns:TestControl runat="server" ID="TestControl1" TestProperty="X:1,Y:2"/>
</div>

Sorry for answering my own question, but I've found out a couple of ways of doing that, and I thought they might be useful for someone else as well :)

To serialize the object in an attribute of the control, you have to define an appropriate TypeConverter, and apply a TypeConverterAttribute to the property type. Here is an example: How to: Implement a Type Converter.

Even easier, you can persist the attribute in the control's content just like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="wbctrlstst._Default" %>
<%@ Register TagPrefix="x" TagName="TestControl" Src="~/TestControl.ascx" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
    <form id="form1" runat="server">
    <div>
        <x:TestControl runat="server" ID="testlist1">
            <TestProperty X="1" Y="42" />
        </x:TestControl>
    </div>
    </form>
</body>
</html>

The only requirement, for what I could see, is that the type must have a default constructor. You can apply the PersistenceModeAttribute to the property to specify that the property should be persisted in the controls's contents, but it looks like it's the default behavior, and this is not strictly needed.

[PersistenceMode(PersistenceMode.InnerProperty)]
public TestData TestProperty {
    get {
        return ViewState["TestProperty"] as TestData;
    }
    set {
        ViewState["TestProperty"] = value;
    }
}