Displaying alert message from code-behind in ASP.NET

By | August 12, 2013

In this post I will explain how to display alert message in ASP.NET from code-behind. We will create a function in the code-behind file to display alert message dynamically.

We will register Javascript dynamically at runtime using ScriptManager.RegisterStratupScript method and display a Javascript alert message from the code-behind.

Create a new ASP.NET Application and add a Button:
<!DOCTYPE html>

<head runat="server">

    <form id="form1" runat="server">
            <asp:Button ID="bthSayHello" runat="server" Text="Say Hello" onclick="bthSayHello_Click" />

Write following in the code-behind page:
public void Alert(Page page, string message)
    string jsString = "alert('" + message + "');";
    ScriptManager.RegisterStartupScript(page, page.GetType(),

protected void bthSayHello_Click(object sender, EventArgs e)
    Alert(this.Page, "Hello");


Public Sub Alert(ByVal page As Page, ByVal message As String)
  Dim jsString As String = "alert('" + message + "');"
End Sub

Protected Sub bthSayHello_Click(sender As Object, e As EventArgs) Handles bthSayHello.Click
     Alert(Me.Page, "Hello")
End Sub

Using RegisterStartupScript we can write any Javascript code at runtime. But as we only need to display an alert message, we are only passing alert() method with our message string as the script parameter in the RegisterStartupScript method. Now, we have a server-side function “Alert” that can be called with a Page and a string object to display a message at runtime.

0 thoughts on “Displaying alert message from code-behind in ASP.NET

  1. Dave

    Worked great… couldn’t find an example like this anywhere. I wanted to be able to call an alert, pass text and data values to, all from a selectedindexchanged event. This worked like a charm.

  2. Kennethhoink

    Your comment is awaiting moderation.

    Regards. I value this.
    new york walk in viagra prescription buy viagra is viagra safe to use viagra for women
    viagra s
    viagra heart healthy
    something that works faster than viagra
    do you prescription for viagra
    viagra package
    <a href="http://www.bryandornan.com/index.php/2010/02/search-engine-optimization-overview/#comments?form2mailerror&inquiry=Business+Development&fname=Howarditala&lname=Howarditala&email=zittel020eejf%40songwriter.net&company=google&phone1=81165293468&phone2=89353854855&phone3=89539955831&comments=+Cheers%21+A+lot+of+data%21++https://nicktambone.com/+mexican+pharmacies+%3Ca+href=%22http://canadianonlinepharmacynnm.com/%22%3Ecanadian+pharmacies-24h%3C/a%3E+cialis+canadian+pharmacy+%3Ca+href=http://canadianpharmaciescubarx.com/%3Ecanada+pharmaceuticals+online%3C/a%3E?form2mailerror&inquiry=Business Development&fname=Brendanlib&lname=Brendanlib&email=stolzenberg334ziz@dutchmail.com&company=google&phone1=86891733399&phone2=88423532441&phone3=83252487268&comments=You actually mentioned this very well! http://viagrawithoutadoctorsntx.com/ viagra 100 pic generic viagra 100mg continuing medil edution lifornia viagra viagra without a doctors prescription“>buy viagra in las vegas


Leave a Reply

Your email address will not be published. Required fields are marked *