Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在asp.net中使用控件状态_Asp.net_.net - Fatal编程技术网

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

如何在asp.net中使用控件状态,asp.net,.net,Asp.net,.net,下面是我在自定义控件中使用控件状态的简单代码 [DefaultProperty("Text")] [ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")] public class WebCustomControl1 : WebControl { [Bindable(true)] [Category("Appearance")] [DefaultVal

下面是我在自定义控件中使用控件状态的简单代码

[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;
    }
}
[DefaultProperty(“文本”)]
[ToolboxData(“”)
公共类WebCustomControl1:WebControl
{
[可装订(真实)]
[类别(“外观”)]
[默认值(“”)
[可本地化(正确)]
公共字符串文本
{
获取{返回文本;}
设置{text=value;}
}
私有字符串文本;
受保护的覆盖无效渲染内容(HtmlTextWriter输出)
{
输出。写入(文本);
}
受保护的覆盖无效OnInit(System.EventArgs e)
{
碱基.奥尼特(e);
第页要求控制状态(本);
}
受保护的覆盖对象SaveControlState()
{
object baseSate=base.SaveControlState();
返回新对(baseSate,Text);
}
受保护的覆盖无效LoadControlState(对象保存状态)
{
Pair value=将数据状态保存为Pair;
文本=值。秒;
}
}
但它似乎不起作用。。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

确定指定的控件对象是否已注册以参与控件状态管理`

但是您应该调用
RegisterRequiresControlState

将控件注册为其控件状态必须保持的控件


能否在.aspx页面上显示控件的标记代码