Show success / error message after postback

1

I have the following functions on my page, when I call them via javascript, they work perfectly:

function msgSucesso(msg) {
    toastr.success(msg);
}

function msgErro(msg) {
    toastr.error(msg);
}

So, I have a button on my aspx page that calls an event from the server:

<asp:Button ID="btnSalvar" runat="server" OnClick="btnSalvar_Click" Text="Salvar" />

No cobehind:

protected void btnSalvar_Click(object sender, EventArgs e)
{
    try
    {
        // código para salvar
    }
    catch (Exception ex)
    {
        // erro
    }
}

I would like to call the functions msgSucesso or msgErro when everything works out or when it fails, respectively, when running the server event. How to do this? I know it's easy, but I'm used to ASP.NET MVC which is quite different.

    
asked by anonymous 30.05.2015 / 20:52

1 answer

1

Try to use the ClientScript.RegisterStartupScript method:

protected void btnSalvar_Click(object sender, EventArgs e)
{
    try
    {
        // código para salvar
        ClientScript.RegisterStartupScript(this.GetType(), "sucesso", "msgSucesso('Sucesso ao salvar')", true);
    }
    catch (Exception ex)
    {
        // erro
        ClientScript.RegisterStartupScript(this.GetType(), "sucesso", "msgErro('Erro ao salvar')", true);    
    }
}
    
30.05.2015 / 20:56