0

I have a question and I can not find the right terms to do a reasoned search and solve the question.

Let's see, when I'm creating a page, at some point I need to create a WebUserControl and defer something like state = "true" (like the text of the lables) inside the html tag so that as soon as the page loads , Whether or not that control is subsequently edited in code.

<MyControls:Teste Id="aaa" runat="server" state="false"/>

The test control code is as follows: (The HTML page of this control is blank, it only has the header)

public partial class WebUserControls_WUC_Tect : System.Web.UI.UserControl
{
    private static bool state ;
    public bool State 
    {
        get { return state ; }
        set { state = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

Problem:

Whenever the page returns to the server and is reloaded, the state variable is always set to false or true depending on the initial state I passed, what I intended was for this variable to be loaded only once at the beginning of the page and then Could only be changed by codebeind.

I am grateful for your suggestions.

greetings Patrick Veiga

pmmivv
  • 1
  • 1

1 Answers1

0

You need to use the ViewState to store the property value to keep the persistent value saved.

public partial class WebUserControls_WUC_Tect : System.Web.UI.UserControl
{
    private static bool state ;
    public bool State 
    {
        get
        {
        if (ViewState["MyState"] == null)
        {
            ViewState["MyState"] = false;
        }
        return (bool)ViewState["MyState"];
        }
        set
        {
        ViewState["MyState"] = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}
KH1229
  • 45
  • 6