在ASP.NET用户控件露出复杂属性 [英] Exposing a complex property in an ASP.NET user control

查看:132
本文介绍了在ASP.NET用户控件露出复杂属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想揭露,从一个自定义的ASP.NET用户控件的复杂属性,以这样一种方式,它可以从aspx页面

控制标记设置

事情是这样的:

 公共类TESTDATA {
    公众诠释X;
    公众诠释Ÿ;
}公共部分类系统testControl:System.Web.UI.UserControl {    公共TESTDATA TestProperty {
        获得{
            返回的ViewState [TestProperty]作为TESTDATA;
        }
        集合{
            的ViewState [TestProperty] =值;
        }
    }
}

然后在包含我想有类似的控制页面的.aspx文件:

 < D​​IV>
    < testns:系统testControl =服务器ID =TestControl1TestProperty =X:1,Y:2/>
< / DIV>


解决方案

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

要序列化对象的控制的一个属性,你必须定义一个适当的的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;
    }
}

这篇关于在ASP.NET用户控件露出复杂属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆