I have a function in the code behind of an aspx page made with Visual Studio that is called from javascript like this:
function cerrar(id) {
document.getElementById('<%= btnBloquea.ClientID()%>').click();
alert("Curso " + id + " bloqueado correctamente.");
}
The problem is that the btnBloquea function sometimes gives an error and is redirected to the error page (I already have it under control) but whether or not the javascript code gives an error, alert
it is ALWAYS shown even if the function has an error since it is executed from asynchronous way.
The button definition:
<asp:Button id="btnBloquea" runat="server" OnClick="btnBloquea_Click" CssClass="hidden"/> //CssCLass hidden es simplement un display:none
And of the function in its minimum expression:
Protected Sub btnBloquea_Click(sender As Object, e As EventArgs)
Dim idEvento As String
Dim cerrado As Boolean
idEvento = hdngroupid.Value()
cerrado = False
'Aquí vienen varios cálculos para si realmente se puede o no cerrar.
If Not cerrado Then
'Aquí me voy a una página de error y NO quiero mostrar ningún alert.
Else
'Aquí todo ha ido bien y quiero mostrar el alert.
End If
End Sub
My questions are:
- How can I alert the user only if the result of the function is correct?
- Is there a way to launch the alert only when it
click()
has finished? - Maybe I can create an alert from the code behind function?
To redirect before a problem you would use the Response.Redirect(), this way you will not return to the page that has the close() function
To execute javascript code, use the
ScriptManager.RegisterStartupScript Method