我想在点Button后让它5秒内失效,请用AJAX。<script runat="server">
      void WaitFiveSeconds(object o, EventArgs e)
      {
          //这里怎么写?
          System.Threading.Thread.Sleep(5000);
          Button1.Enabled = false;
      }
    </script><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>AJAX</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" >
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>        
        <asp:Button ID="Button1" runat="server"
            Text = "Do something"  OnClick="WaitFiveSeconds" /><br />
        <asp:Label ID="Label1" runat="server" />        
        </ContentTemplate>        
        </asp:UpdatePanel>
        <asp:UpdateProgress ID="UpdateProgress1" runat="server"
            AssociatedUpdatePanelID="UpdatePanel1">
        <ProgressTemplate>
        <div style="position:absolute; left:200px; top:150px;
              padding:40px;">
              </div>
            Loading,Please wait for 5 seconds ...
            </ProgressTemplate>
            </asp:UpdateProgress>
    </form>
</body>
</html>

解决方案 »

  1.   

    你点击后设置Button的Enabled = False
    然后用Ajax转到后台,在后台System.Threading.Thread.Sleep(5000);
    再返回到前台,设置Button的Enabled = true
      

  2.   

    这个demo<%@ Page Language="C#" %><script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                设置n秒钟后失效(this.Button1, 5);
        }    private void 设置n秒钟后失效(WebControl wc, int n)
        {
            wc.Page.ClientScript.RegisterStartupScript(wc.GetType(), "disable", "setTimeout(function(){document.getElementById('" +
                wc.ClientID + "').disabled='disabled';}," + (n * 1000).ToString() + ");", true);
        }
    </script><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>demo</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        </div>
        <asp:Button ID="Button1" runat="server" Text="Button" />
        </form>
    </body>
    </html>
      

  3.   

    我是在页面第一次加载时,而你可以在按钮的后台click事件中调用那个方法。
      

  4.   

    UpdatePanel? 那么输出脚本的代码应该改为通过 scriptmanager 注册,而不是通过Page.ClientScript 注册,这样才能执行。
      

  5.   

    嗯,这是修改后的asp.net ajax例子:
    <%@ Page Language="C#" %><script runat="server">    private void 设置n秒钟后失效(WebControl wc, int n)
        {
            ScriptManager.RegisterStartupScript(wc, wc.GetType(), "disable", "setTimeout(function(){document.getElementById('" +
                wc.ClientID + "').disabled='disabled';}," + (n * 1000).ToString() + ");", true);
        }    protected void Button1_Click(object sender, EventArgs e)
        {
            设置n秒钟后失效(this.Button1, 5);
        }</script><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>demo</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            </ContentTemplate>
        </asp:UpdatePanel>
        </form>
    </body>
    </html>
      

  6.   

    sp1234:
       我试验了你给的demo,效果是点Button5秒以后失效,而非5秒以内一直失效,5秒后可以再点。
      

  7.   


    呵呵,这个呀。改一下:<%@ Page Language="C#" %><script runat="server">    private void 设置n秒钟后生效(WebControl wc, int n)
        {
            ScriptManager.RegisterStartupScript(wc, wc.GetType(), "disable", "setTimeout(function(){document.getElementById('" +
                wc.ClientID + "').disabled=null;}," + (n * 1000).ToString() + ");", true);
        }    protected void Button1_Click(object sender, EventArgs e)
        {
            this.Button1.Enabled = false;
            设置n秒钟后生效(this.Button1, 5);
        }</script><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>demo</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            </ContentTemplate>
        </asp:UpdatePanel>
        </form>
    </body>
    </html>