本文介绍了请帮我我的朋友们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我该如何用C#中的按钮做两项不同的事情
how can I do two different thing with a button in C#
推荐答案
protected void btnSave_Click(object sender, EventArgs e)
{
if(btnSave.Text == "Save")
{
btnSave.Text = "Update";
SaveData();
}
else
{
btnSave.Text = "Save";
UpdateData();
}
}
如果您现在明白我的意思,则可以扩展此行为并在同一按钮上执行多项操作.使用ViewState在每个click事件上保存一些变量值,然后调用相应的函数.
谢谢
If you are getting my point now, you can extend this behavior and do multiple thing on the same button. Use ViewState to save some variable value on each click event and call the appropriate function.
Thanks
private delegate string LongTaskWorkHandler(string value1, string value2);
private void button1_click(object sender, EventArgs e)
{
LongTaskWorkHandler longTaskDelegate = DoLongTask;
IAsyncResult ar = longTaskDelegate.BeginInvoke("THE", "CODEPROJECT", null, null);
while(true)
{
Console.Write(".");
if(ar.AsyncWaitHandle.WaitOne(50, false))
{
Console.WriteLine("Can get the result now.");
break;
}
}
// Waits until the delegate has completed its work.
string result = longTaskDelegate.EndInvoke(ar);
MessageBox.Show(result);
}
// running in a background thread.
private string DoLongTask(string value1, string value2)
{
string output = null;
try
{
output = LongTask(value1, value2);
}
catch (Exception ex)
{
output = ex.Message;
}
return output;
}
private string LongTask(string value1, string value2)
{
Console.WriteLine("LongTask started.");
Thread.Sleep(5000);
Console.WriteLine("LongTask completed.");
return String.Format("{0} {1}", value1, value2);
}
这篇关于请帮我我的朋友们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!