21 April 2011

Disabling the button while the user wait

WebForm1.aspx
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebService1.WebForm1" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button runat="server" ID="cmd1" Text="test..." Height="40px" Width="246px" />
        <p>
            <asp:Label runat="server" ID="lbl1" />
        </p>
    </div>
    </form>
</body>
</html>
WebForm1.aspx.vb (il trucco è la Me.cmd1.Attributes.Add alla riga 6):
Public Partial Class WebForm1
    Inherits System.Web.UI.Page

    Private Sub WebForm1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Me.cmd1.Attributes.Add("onclick", "this.value='Please wait...';this.disabled = true;" + ClientScript.GetPostBackEventReference(Me.cmd1, ""))

    End Sub

    Protected Sub cmd1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmd1.Click

        System.Threading.Thread.Sleep(1000 * 10)
        Me.lbl1.Text = Date.Now.ToString

    End Sub

End Class

Trovato qui.