Click here to Skip to main content
15,885,839 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hai everybody,
I am faizel and i am right now doing a website. i just want to know whether there is any page exit function just like page_load() function. can any one tell me a way to reset a counter using static variable when i exit a webpage. i tried to do it in the page load function but when i click a button it resets .i want the counter to reset when the page is exited.here is my code.
C#
static int x;

    protected void Page_Load(object sender, EventArgs e)
    {
x=0;
}
//button click
protected void Button8_Click(object sender, EventArgs e)
    {
        
        x = x + 6;
        
    }
 protected void Button7_Click(object sender, EventArgs e)
    {
        x = x - 6;
        
    }
Posted
Updated 6-Jun-23 12:39pm

Hi,

add ispostback condition in page_load method.

C#
protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
        x=0;
}



It will solve your problem.

Thanks.
 
Share this answer
 
Comments
faizel s 2-Aug-13 7:31am    
thanks for trying to help me. i have tried your method but it isnt working.is there any other way to do that
Harshil_Raval 2-Aug-13 7:42am    
Why you want to clear value? Is this value uses in other page also? When you came back to page, value became 0. I have tried it.
faizel s 2-Aug-13 7:55am    
each time i click a button the value of x becomes zero . so this is not functioning as a counter .i dont want that to happen..
Harshil_Raval 2-Aug-13 8:03am    
Hi, if !IsPostBack is added then that will call at the time of page load. After that while any number of postback it would not call. Please check again by using if(!IsPostBack) x=0; It should work.
faizel s 3-Aug-13 1:47am    
thank you harshival for helping.. now the method works. it was the problem with page i was working on..when i did in another page,it worked...Anyway thank you for helping me..god bless you...tc
yes there is write page_unload method where you can do your coding when page is about to unload your method will be called...regards :)
 
Share this answer
 
Comments
faizel s 2-Aug-13 8:01am    
can you please tell me how to write the function
Dholakiya Ankit 2-Aug-13 8:06am    
http://www.google.co.in/#biw=1024&bih=677&sclient=psy-ab&q=page+unload+event+in+asp.net+c%23&oq=page+unlod&gs_l=hp.3.2.0i13l4.760.2454.0.4930.10.9.0.1.1.0.243.1708.0j6j3.9.0....0...1c.1.23.psy-ab..0.10.1660.3JNdhZufiKg&pbx=1&bav=on.2,or.r_qf.&bvm=bv.50165853%2Cd.bmk%2Cpv.xjs.s.en_US.seW1cfrvSKg.O&fp=221d55d00cd2fdba
Dholakiya Ankit 2-Aug-13 8:06am    
check this link
Use page_unload event

Thanks,
Ambesha
 
Share this answer
 
Maybe using the Page_Unload event to handle logic when a page is exited. The event is raised after the page has been fully rendered and sent to the client

C#
static int x;

protected void Page_Load(object sender, EventArgs e)
{
    x = 0;
}

protected void Button8_Click(object sender, EventArgs e)
{
    x = x + 6;
}

protected void Button7_Click(object sender, EventArgs e)
{
    x = x - 6;
}

protected void Page_Unload(object sender, EventArgs e)
{
    x = 0; 
}
 
Share this answer
 
v2
Comments
Graeme_Grant 5-Jun-23 21:28pm    
It has been 9 years since he asked the question, do you really think that he still needs help? Please focus on current questions.
Shahin Khorshidnia 6-Jun-23 3:58am    
You are right. I didn't notice the date.
However, the question was on top of the asked questions.
Graeme_Grant 6-Jun-23 5:41am    
That was because someone viewed it... always check the date.
Shahin Khorshidnia 6-Jun-23 8:04am    
Thank you. However, that wasn't a big deal. I believe it was not necessary to tell someone what to do and what not to do with direct sentences.
Richard Deeming 6-Jun-23 3:45am    
Aside from the age of the question, Page_Unload was already mentioned in solution 2.

Also, using static variables in ASP.NET is never the right thing to do, and just demonstrates that you don't understand how ASP.NET works.

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