简体   繁体   English

如何 JSON 序列化 class 派生自 Web 控制 ZA2F2ED4F8EBC2CBB4C21A29DC40AB6D1

[英]How to JSON serialize class derived from Web Control class

Does anyone know how to serialize a custom control to JSON that is derived from another web control.有谁知道如何将自定义控件序列化为从另一个 web 控件派生的 JSON 控件。 I keep getting the error that the base class is not serializable.我不断收到基本 class 不可序列化的错误。 Here is the code for the derived class这是派生的 class 的代码

public class BaseWidget : CompositeControl
{
    protected Label _lblHeader;
    protected Button _editButton;
    protected HtmlInputHidden _idField;

    /// <summary>
    /// The Unique identified that identifies this widget
    /// </summary>

    public int ControlID
    {
        get
        {
            EnsureChildControls();
            int i = -1;
            int.TryParse(_idField.Value, out i);
            return i;
        }
        set
        {
            EnsureChildControls();
            _idField.Value = value.ToString();
        }
    }

    /// <summary>
    /// Allow the user to edit the widget
    /// </summary>
    public bool AllowEdit
    {
        get;
        set;
    }


    public string Title
    {
        get
        {
            EnsureChildControls();
            return _lblHeader.Text;
        }
        set
        {
            EnsureChildControls();
            _lblHeader.Text = value;
        }
    }



    public string CallbackFunction
    {
        get;
        set;
    }


    protected Panel Header
    {
        get;
        set;
    }

    /// <summary>
    /// The Main Control of the widget
    /// </summary>
    protected Panel Content
    {
        get;
        set;
    }

    protected Panel Edit
    {
        get;
        set;
    }

    protected Panel Body
    {
        get;
        set;
    }

    /// <summary>
    /// The tag that the control is associated with
    /// </summary>
    protected override HtmlTextWriterTag TagKey
    {
        get
        {
            return HtmlTextWriterTag.Li;
        }
    }


    public BaseWidget()
    {
        this.ControlID = 0;
    }}

I actually don't even need to serialize any of the attributes in the base class.实际上,我什至不需要序列化基本 class 中的任何属性。 I just need the control id and the title associated serialized.我只需要序列化的控件 id 和关联的标题。 Is there any way of doing this when Web Control is not serializable.当 Web 控件不可序列化时,有什么办法可以做到这一点。 I tried DataContractJsonSerializer and JavascriptSerializer with no luck because the WebControl class is not serializable.我尝试了 DataContractJsonSerializer 和 JavascriptSerializer,但没有成功,因为 WebControl class 不可序列化。

On the DataContractJsonSerializer you can define a surrogate which is serialized in place of the control (search for IDataContractSurrogate for more information).在 DataContractJsonSerializer 上,您可以定义一个替代控件进行序列化的代理(搜索 IDataContractSurrogate 以获取更多信息)。 But if all you need is to serialize those two properties, then it'll likely be simpler for you to simply create a data class (DTO?) with those properties and serialize it instead.但是,如果您只需要序列化这两个属性,那么使用这些属性简单地创建数据 class (DTO?) 并将其序列化可能会更简单。

public class BaseWidgetDTO
{
    public int ControlID { get; set; }
    public string Title { get; set; }
}

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

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