需要通过按钮查询数据库,但是查询时间比较长,大约3秒实现功能如下:点击按钮后,马上令按钮的Enabled属性为false。label1.Text = "正在查询";完成查询后,令按钮的Enabled属性为true。label1.Text = "查询成功";
以上时间就是通过一个click事件来实现的,应该属于典型的异步问题,希望大家了解的帮帮忙本问题主要是为了了解与实现异步调用或类似问题,也算是技术讨论了

解决方案 »

  1.   


    using System;
    using System.Net;
    using System.Net.Sockets;namespace Examples.AdvancedProgramming.AsynchronousOperations
    {
        public class BlockUntilOperationCompletes
        {
            public static void Main(string[] args)
            {
                // Make sure the caller supplied a host name.
                if (args.Length == 0 || args[0].Length == 0)
                {
                    // Print a message and exit.
                    Console.WriteLine("You must specify the name of a host computer.");
                    return;
                }
                // Start the asynchronous request for DNS information.
                // This example does not use a delegate or user-supplied object
                // so the last two arguments are null.
                IAsyncResult result = Dns.BeginGetHostEntry(args[0], null, null);
                Console.WriteLine("Processing your request for information...");
                // Do any additional work that can be done here.
                try 
                {
                    // EndGetHostByName blocks until the process completes.
                    IPHostEntry host = Dns.EndGetHostEntry(result);
                    string[] aliases = host.Aliases;
                    IPAddress[] addresses = host.AddressList;
                    if (aliases.Length > 0)
                    {
                        Console.WriteLine("Aliases");
                        for (int i = 0; i < aliases.Length; i++)
                        {
                            Console.WriteLine("{0}", aliases[i]);
                        }
                    }
                    if (addresses.Length > 0)
                    {
                        Console.WriteLine("Addresses");
                        for (int i = 0; i < addresses.Length; i++)
                        {
                            Console.WriteLine("{0}",addresses[i].ToString());
                        }
                    }
                }
                catch (SocketException e)
                {
                    Console.WriteLine("An exception occurred while processing the request: {0}", e.Message);
                }
            }
        }
    }
      

  2.   

    听说有一种方式可以模仿ajax的异步处理,各位朋友有没有了解的啊。大体思路就是通过一个按钮,执行一个事件,然后在这个事件中写入js代码,在js代码中,执行另外一个事件,如此打到异步执行的效果。比如本例,就可以使程序先显示label1.Text = "正在查询"; 
    然后调用js代码
    执行查询时间,查询完成后,显示
    label1.Text = "查询成功"; 
      

  3.   

    这个问题我已经解决了,我给大家公布一下答案:
        protected void btnTest21_Click(object sender, EventArgs e)
        {
            lblTest21.Text = "第一次";
            btnTest21.Enabled = false;
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>document.getElementById('btnTest22').click();</script>");
        }
        protected void btnTest22_Click(object sender, EventArgs e)
        {
            Thread.Sleep(3000);
            lblTest22.Text += "第二次";
        }