Click here to Skip to main content
13,895,277 members
Rate this:
 
Please Sign up or sign in to vote.
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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:
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;
    }
}
   
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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"; }
   
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web02 | 2.8.190306.1 | Last Updated 11 Jan 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100