Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
two buttons for variable verification
the FlgBtn class is initialized and the flg variable is always false

button Enable set to true flg
button Test check the value (it had to remain true)

What I have tried:

<form id="form1" runat="server">
        
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

        <div>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:Button ID="Button1" runat="server" Text="Enable" onclick ="Enable"/>
                </ContentTemplate>
            </asp:UpdatePanel>

            <asp:UpdatePanel ID="UpdatePanel3" runat="server">
                <ContentTemplate>                   
                    <asp:Button ID="Button3" runat="server" Text="Test" OnClick = "Test" />                    
                </ContentTemplate>                
            </asp:UpdatePanel>

        </div>





namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        private FlgButton flgbtn = new FlgButton();
        bool TestBtn;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
            }
        }

        protected void Enable(object sender, EventArgs e)
        {
            flgbtn.SetFlg(true);
        }

        protected void Disable(object sender, EventArgs e)
        {
            flgbtn.SetFlg(false);
        }

        protected void Test(object sender, EventArgs e)
        {
            TestBtn = flgbtn.GetFlg();
        }
    }

    public class FlgButton
    {
        private bool flg;

        /*
        public FlgButton()
        {
            flg = false;
        }
        */

        public void SetFlg(bool value)
        {
            flg = value;
        }

        public bool GetFlg()
        {
            return flg;
        }
    }
Posted
Updated 11-Jan-19 4:57am

The value of your class's fields will not be persisted between requests, even if you use an UpdatePanel. For every request, a new instance of the class will be created, and it will be thrown away once the response has been returned to the user.

If you want to persist data between requests, you need to store it in a different way. In this case, the best option would be to use ViewState.
Beginner's Guide To View State [^]

Eg:
C#
public partial class WebForm1 : System.Web.UI.Page
{
    private FlgButton flgbtn;
    
    public WebForm1()
    {
        flgbtn = new FlgButton(ViewState, "flgbtn.");
    }
    
    private bool TestBtn
    {
        get { return (bool?)ViewState["TestBtn"] ?? false; }
        set { ViewState["TestBtn"] = value; }
    }
    
    ...
}

public class FlgButton
{
    private readonly StateBag _viewState;
    private readonly string _viewStatePrefix;
    
    public FlgButton(StateBag viewState, string viewStatePrefix)
    {
        _viewState = viewState;
        _viewStatePrefix = viewStatePrefix;
    }
    
    private bool Flg
    {
        get { return (bool?)_viewState[_viewStatePrefix + "Flg"] ?? false; }
        set { _viewState[_viewStatePrefix + "Flg"] = value; }
    }
    
    public void SetFlg(bool value)
    {
        Flg = value;
    }
    
    public bool GetFlg()
    {
        return Flg;
    }
}
 
Share this answer
 
v2
thanks for the info, I don't know the mechanisms of ViewState

I have add
EnableViewState="true"
to page

the compiler sign two error

private FlgButton flgbtn = new FlgButton(ViewState, "flgbtn.");


A field initiator cannot refer to the 'Control.ViewState' property, method or non-static field.

Another is the "]"
get { return (bool?)_viewState[_viewStatePrefix + "Flg"; }


this test not resolve

get { return (bool?)_viewState[_viewStatePrefix + "Flg"] }

or
<pre>get { return (bool?)_viewState[_viewStatePrefix] + "Flg"; }
 
Share this answer
 
Comments
Richard Deeming 11-Jan-19 11:04am    
This should have been posted as a comment to my solution, not as a new solution.

I've corrected my solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900