5

Below is my simple code to use control state in a custom control,

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

But it doesn't seem to work.. The SaveControlState and LoadControlState are not firing. can someone help me..?

Below is the aspx Code. Here is where i use the custom control.

    <%@ 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>
1
  • can you show the markup code for the control on .aspx page Nov 11, 2011 at 7:41

1 Answer 1

7

You've called RequiresControlState

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

But you should call RegisterRequiresControlState

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.