DerekWatling
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
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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_Cl ick(this); " />
Protected Sub btnLogin_Click(sender as object, e as EventArgs) Handles btnLogin.Click
...
End Sub
<script type="text/javascript">
function btnLogin_Click(btn)
{
btn.value = 'Wait...';
btn.disabled = true;
document.body.style.cursor
__doPostBack('<%=btnLogin.
}
</script>
<asp:Button ID="btnLogin" runat="server" Text="Login" OnClientClick="btnLogin_Cl
Protected Sub btnLogin_Click(sender as object, e as EventArgs) Handles btnLogin.Click
...
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
If IsPostBack Then
If Request.Params("__EVENTTAR
btnLogin_Click(sender, e)
End If
End Sub