如何 JSON 序列化从 Web Control 类派生的类

发布于 2024-11-09 15:26:34 字数 1962 浏览 3 评论 0原文

有谁知道如何将自定义控件序列化为从另一个 Web 控件派生的 JSON。我不断收到基类不可序列化的错误。这是派生类的代码,

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;
    }}

实际上我什至不需要序列化基类中的任何属性。我只需要序列化关联的控件 ID 和标题。当 Web Control 不可序列化时,有什么办法可以做到这一点。我尝试了 DataContractJsonSerializer 和 JavascriptSerializer 但没有成功,因为 WebControl 类不可序列化。

Does anyone know how to serialize a custom control to JSON that is derived from another web control. I keep getting the error that the base class is not serializable. Here is the code for the derived 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. I just need the control id and the title associated serialized. Is there any way of doing this when Web Control is not serializable. I tried DataContractJsonSerializer and JavascriptSerializer with no luck because the WebControl class is not serializable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

手长情犹 2024-11-16 15:26:34

在 DataContractJsonSerializer 上,您可以定义一个替代控件进行序列化的代理(搜索 IDataContractSurrogate 以获取更多信息)。但是,如果您需要的只是序列化这两个属性,那么只需使用这些属性创建一个数据类(DTO?)并对其进行序列化可能会更简单。

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

On the DataContractJsonSerializer you can define a surrogate which is serialized in place of the control (search for IDataContractSurrogate for more information). 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.

public class BaseWidgetDTO
{
    public int ControlID { get; set; }
    public string Title { get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文