Link to home
Create AccountLog in
Avatar of DerekWatling
DerekWatlingFlag for South Africa

asked on

Button_Click: Execute JavaScript then standard post back

I have my security as part of the Master page with Login/Logout buttons. When a user logs in I want to first execute some JavaScript to disable the button and change the cursor style to "wait" and then PostBack and execute the btnLogin_Click server side code.  So far I have the code below, but need to know which button caused the PostBack so I only execute the code in the "If" statement if btnLogin was clicked and not any other button on the page. Is this the right approach or is there a simpler way to handle this situation?


.aspx
--------------------------------------------------------------------
<asp:Button ID="btnLogin" runat="server" Text="Login" OnClientClick="this.disabled=true;this.value='Wait...';document.body.style.cursor='wait';__doPostBack('btnLogin','');" />
 
.aspx.vb
--------------------------------------------------------------------
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
...
  If IsPostBack Then
    btnLogin_Click(sender, e)
  End If
...
End Sub
 
Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogin.Click
...
End Sub

Open in new window

Avatar of jlj1527
jlj1527
Flag of Viet Nam image

In Page load
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
             If IsPostBack Then
                  If  Request.Params("__EVENTTARGET") = "btnLogin" then
                      btnLogin_Click(sender, e)
            End If

End Sub
Avatar of DerekWatling

ASKER

Everything I have found on the subject says that this method will work for all controls EXCEPT Button and ImageButton! These 2 post back differently to other controls.
ASKER CERTIFIED SOLUTION
Avatar of jlj1527
jlj1527
Flag of Viet Nam image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Try this:
<script type="text/javascript">
    function btnLogin_Click(btn)
    {
        btn.value = 'Wait...';
        btn.disabled = true;
        document.body.style.cursor='wait';
        __doPostBack('<%=btnLogin.UniqueID %>','');
    }
</script>

<asp:Button ID="btnLogin" runat="server" Text="Login" OnClientClick="btnLogin_Click(this);" />

Protected Sub btnLogin_Click(sender as object, e as EventArgs) Handles btnLogin.Click
...
End Sub