加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

如何在asp.net中使用控件状态

发布时间:2020-12-16 07:10:04 所属栏目:asp.Net 来源:网络整理
导读:下面是我在自定义控件中使用控件状态的简单代码, [DefaultProperty("Text")][ToolboxData("{0}:WebCustomControl1 runat=server/{0}:WebCustomControl1")]public class WebCustomControl1 : WebControl{ [Bindable(true)] [Category("Appearance")] [Default
下面是我在自定义控件中使用控件状态的简单代码,

[DefaultProperty("Text")]
[ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]
public class WebCustomControl1 : WebControl
{
    [Bindable(true)]
    [Category("Appearance")]
    [DefaultValue("")]
    [Localizable(true)]
    public string Text
    {
        get { return text; }
        set { text = value; }
    }
    private string text;

    protected override void RenderContents(HtmlTextWriter output)
    {
        output.Write(Text);
    }

    protected override void OnInit(System.EventArgs e)
    {
        base.OnInit(e);
        Page.RequiresControlState(this);
    }

    protected override object SaveControlState()
    {
        object baseSate = base.SaveControlState();
        return new Pair(baseSate,Text);
    }

    protected override void LoadControlState(object savedState)
    {
        Pair value = savedState as Pair;
        text = value.Second;
    }
}

但它似乎不起作用.. SaveControlState和LoadControlState没有触发.有人能帮我吗..?

下面是aspx代码.这是我使用自定义控件的地方.

<%@ Page Language="C#" AutoEventWireup="true" EnableViewState="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<%@ Register Assembly="WebApplication1" Namespace="WebApplication1" TagPrefix="cc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>`enter code here`
<body>
    <form id="form1" runat="server">
    <div>
        <cc1:WebCustomControl1 ID="WebCustomControl1_1"  runat="server" />
        <asp:Button ID="Button1" runat="server" Text="Button" /></div>
    </form>
</body>
</html>

解决方法

你已经调用了RequiresControlState

Determines whether the specified Control object is registered to participate in control state management.`

但是你应该调用RegisterRequiresControlState

Registers a control as one whose control state must be persisted.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读