Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have a button and a function .Now I want to call this function on button click event only once next time I do not want to call this function.


Please let me know how to do it.

Thanking You
Mohd Wasif
Posted

int i=0;
protected void Button1_Click(object sender, EventArgs e)
{
if(i=0)
{
function()
i=1;
}
}
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 8-Jul-11 2:38am    
Did you ever think that such code can be avoided forever?
Look my article on the topic "Wish You Were Here… Only Once"?
If you have a minute, take a look -- very exotic and interesting -- see my solution.
--SA
A very universal method is offered by my article: Wish You Were Here… Only Once[^].

The usage is unusually simple and looks like this:

if (FirstTime.Here)
    ThisWillHappenButOnlyOnce();


where FirstTime.Here is the static method of my class and ThisWillHappenButOnlyOnce is your method (or anything, some block) you want to call only once per you runtime.

Warning! Implementation of this feature has good performance, very reliable and universal but… understanding of how it works can be a mind-bending. :-)
Please try.

You can use my library in any type of .NET project written in any language.

—SA
 
Share this answer
 
Comments
[no name] 8-Jul-11 2:46am    
Good Call. My 5.
Sergey Alexandrovich Kryukov 8-Jul-11 3:09am    
Thank you, Ramalinga.
--SA
[no name] 8-Jul-11 8:40am    
It's my Pleasure.
Uday P.Singh 8-Jul-11 2:53am    
Its very good but may be too much for OP, nevertheless my 5!
Sergey Alexandrovich Kryukov 8-Jul-11 3:09am    
Thank you, Uday. Well, it depends. If OP wants just to use the library, this "too much" is reduced to the code pattern I demonstrate in the above 2-line codelet. (There are more advanced options and different debug version, but this one is the basic.) If one wants to understand it.... Oh... well nothing terrible, my article explains it.
--SA
Hi,
it can be done easily using a flag.
int i=0;//it is set in the !ispostback 

protected void Button1_Click(object sender, EventArgs e)
{
if(i=0)
{
function()//call our function
i=1;
}

}

try this.if nay doubt pls comment on this.
 
Share this answer
 
v2
Comments
Toniyo Jackson 8-Jul-11 2:21am    
Use code block for code
Sergey Alexandrovich Kryukov 8-Jul-11 2:35am    
Did you see my article on the topic "Wish You Were Here… Only Once"?
If you have a minute, take a look -- very exotic.
--SA
Toniyo Jackson 8-Jul-11 3:58am    
I saw your article now. Thanks for sharing. Its very good and bookmarked also :)
Sergey Alexandrovich Kryukov 12-Jul-11 15:04pm    
I saw it and replied. Thanks for you good words, Toniyo.
--SA
use a public bool type variable and check it on button click event.like

C#
public Form1()
        {
            InitializeComponent();
        }
        bool a = true;
        private void button1_Click(object sender, EventArgs e)
        {
            if (a == true)
            {
                //your code
                a = false;
            }
        }
         function()//your function
         {
         }
     
        }
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 8-Jul-11 2:37am    
Did you mean "==" instead of assignment "="? ';' after function(). It all won't even compile! Very bad.
--SA
[no name] 8-Jul-11 2:47am    
My Vote of 1. It shows error as SA said.
Uday P.Singh 8-Jul-11 2:50am    
basic programming mistake!! :(

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