Show success/error message after postback

Asked

Viewed 539 times

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);
}

Hence, I have a button on my aspx page that calls a server event:

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

In the cobehind:

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

I’d like to call in the duties msgSucesso or msgErro when everything goes right or when it goes wrong, 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.

1 answer

1


Try to use the method ClientScript.RegisterStartupScript:

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);    
    }
}
  • It worked, it worked. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.