开发者

ASP.NET - some member variables not working as expected?

I've been working in ASP.NET- just starting out, and I have a pretty simple question. I've got a button, and when you click it, it adds some simple text to a drop-down box. Here's the code:

public partial class _Default : System.Web.UI.Page
{
    private int buttonclickedtimes = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        this.DropDownList1.Items.Add(new System.Web.UI.WebControls.ListItem("Clickin' mah buttonz " + buttonclickedtimes + " times!"));
        buttonclickedtimes++;
    }
}

But buttonclickedtim开发者_运维技巧es always comes up as one. If I make the variable static, it works as intended. But the DropDownList is a member variable and it's clearly properly stateful- as in, working as I expect. I don't understand this behaviour- surely all member variables are either saved or not saved between requests? I'm running in debug mode in VS2010.


No, member variables are not saved between requests, if you want to save it, you need to put it in Viewstate.

The Control's state is saved in Viewstate (if you have viewstate enabled) so that's why it's being saved between requests.

To save in Viewstate, add the following to a Pre_Render method:

ViewState.Add("buttonClickTracker", buttonclickedtimes);

And then access it:

buttonclickedTimes = Viewstate["buttonClickTracker"];


I assume that you are using asp.net webforms. Here every time you make a post back to the server it's basically a new request and page will be loaded and initialized again.

So If you want to preserve data between multiple requests then you have to use some mechanism like sesison variables to store it.

for eg. Session["ButtonClickedTimes"] +=1; will increment it after every request.


This comes down to a page life cycle "issue". What happens is that the page gets constructed (and your buttonclickedtimes get set to the initial value of 1). Then it goes through the page lifecycle (init, load, event handeling, rendering, etc) and the memory allocation for the variable gets released. However, when the variable is static, the server knows to hang onto the memory allocation for the variable and any changes to it persist. However, that static variable is persisted for that page. Meaning that for x users who click your button, that variable will be incremented x times.

If you want the variable to persist for a single user instance, you could use ViewState or possibly Session state to save the information.

Hope this is helpful.


Since web application are stateless ... it initiate the variable each time it reloads the page. Thats why we have Page.IsPostback check.

For state, you should either use viewstate or session state ....

Static classes have one intance and hence they retain the value irrespective of page loads...

I hope this helps you.


In ASP.NET the Page is instantiated every time it is posted.

So your local variable is 1 because the _Default class is a new class each time you click the button.

If you want to save the state, you can use the ViewState object or Session object.

I recommend you read some of Dino Esposito's ASP.NET overviews on MSDN, he`s good at explaining all of this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜