Identify a complex property in an ASP.NET user control

I would like to provide a complex property from a custom ASP.NET user element so that it can be set from a control tag on an aspx page .

Something like that:

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 the page containing the control, I would like to have something like:

<div>
    <testns:TestControl runat="server" ID="TestControl1" TestProperty="X:1,Y:2"/>
</div>
+3
source share
1 answer

Sorry for the answer to my own question, but I found a couple of ways to do this, and I thought that they could be useful for someone else.

, TypeConverter TypeConverterAttribute. : . .

, :

<%@ 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>

, , , . PersistenceModeAttribute , , , , , .

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

Source: https://habr.com/ru/post/1732516/


All Articles