Exposing a complex property in an ASP.NET user control(在 ASP.NET 用户控件中公开复杂属性)
问题描述
我想从一个自定义的 ASP.NET 用户控件中公开一个复杂的属性,可以从 aspx 页面中的控件标记中设置它.
类似这样的:
公共类 TestData {公共诠释 X;公共int Y;}公共部分类TestControl:System.Web.UI.UserControl {公共测试数据测试属性 {得到 {返回 ViewState["TestProperty"] 作为 TestData;}放 {ViewState["TestProperty"] = 值;}}}然后在包含控件的页面的 .aspx 文件中,我希望有类似的内容:
<testns:TestControl runat="server" ID="TestControl1" TestProperty="X:1,Y:2"/></div> 解决方案 很抱歉回答了我自己的问题,但我已经找到了几种方法,我认为它们可能对其他人也有用:)
要序列化控件属性中的对象,您必须定义适当的 TypeConverter,并将 TypeConverterAttribute 应用于属性类型.下面是一个示例:如何:实现类型转换器.
更简单的是,您可以像这样将属性持久化到控件的内容中:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="wbctrlstst._Default" %><%@注册TagPrefix="x" TagName="TestControl" Src="~/TestControl.ascx" %><html xmlns="http://www.w3.org/1999/xhtml" ><身体><form id="form1" runat="server"><x:TestControl runat="server" ID="testlist1"><TestProperty X="1" Y="42"/></x:TestControl></div></表格></身体></html>就我所见,唯一的要求是该类型必须具有默认构造函数.您可以将 PersistenceModeAttribute 应用于该属性,以指定该属性应持久保存在控件的内容中,但它看起来像是默认行为,并非绝对需要.
[PersistenceMode(PersistenceMode.InnerProperty)]公共测试数据测试属性 {得到 {返回 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 用户控件中公开复杂属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 ASP.NET 用户控件中公开复杂属性
基础教程推荐
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
