如何在函数执行一次后将其删除
我有一个要在单选按钮检查事件下执行的函数,但只需执行一次。在随后的单选按钮检查事件中,不应调用我的函数。
void rb_CheckedChanged(x, y)
{
LoadSomeData(); //need to load only once.
// rest of the code; to be executed every single time..
}
注意:
我希望仅在单选按钮事件下执行
LoadSomeData()
。我可以设置一个整数成员变量,并执行 count++、n 检查 if == 1 等。或者布尔标志。这不是我想要的。
谢谢..
I have a function to be executed under a radio button checked event, but it has to be done only once. In subsequent radio button checked events, my function shouldn't be called.
void rb_CheckedChanged(x, y)
{
LoadSomeData(); //need to load only once.
// rest of the code; to be executed every single time..
}
Note:
I want
LoadSomeData()
to be executed only under radio button event.I can set an integer member variable, and do count++, n check if == 1 etc. Or a boolean flag. That's not what I'm looking for.
Thanks..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(5)
向日葵2024-12-06 11:11:16
您可以设置隐藏字段并使用它作为标志来确定是否应该再次运行 LoadSomeData
。类似于:
void rb_CheckedChanged(x, y)
{
if (hiddenField.Value == "False")
{
LoadSomeData(); //need to load only once.
hiddenField.Value = true.ToString();
}
else
{
// Do nothing.
}
// rest of the code; to be executed every single time..
}
在您的页面中:
<asp:HiddenField ID="hiddenField" Value="False" />
乱世争霸2024-12-06 11:11:16
遗失的美好2024-12-06 11:11:16
我会选择第二个选项,但你可以使用委托:
public event Action doLoadSomeData;
void onLoad(sender, args)
{
doLoadSomeData += LoadSomeData();
}
void rb_CheckedChanged(x, y)
{
if (doLoadSomeData != null)
{
doLoadSomeData(); //need to load only once.
}
doLoadSomeData = null;
// rest of the code; to be executed every single time..
}
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您可以做的是创建一个要在事件中调用的函数名称列表,然后从列表中获取方法名称,使用反射调用您感兴趣的方法,然后从列表中删除该方法名称...下次,当再次调用该事件时,该方法不会在列表中之前被执行..类似于:
then
您可以从以下位置获取有关动态方法调用的信息: http://www.codeproject.com/KB/cs/CallMethodNameInString.aspx
What you can do is create a list of function names that you would like to call in your event, then get the method name from list, using reflection call the method you are interested in and then remove that method name from the list... Next time, when the event is called again, the method won't be executed before it won't be in the list.. something like:
then
You can get information about dynamic method invoking from: http://www.codeproject.com/KB/cs/CallMethodNameInString.aspx