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>
<html>

<head runat="server">
    <title></title>
</head>

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

</html>
Write following in the code-behind page:
C#:
public void Alert(Page page, string message)
{
    string jsString = "alert('" + message + "');";
    ScriptManager.RegisterStartupScript(page, page.GetType(),
            "MyApplication",
            jsString,
            true);
}

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

VB.NET:

Public Sub Alert(ByVal page As Page, ByVal message As String)
  Dim jsString As String = "alert('" + message + "');"
  ScriptManager.RegisterStartupScript(page,
  page.GetType(),
  "MyApplication",
  jsString,
  True)
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.

    Reply
  2. QuentinBak

    Your comment is awaiting moderation.

    This is nicely said. .
    https://nicktambone.com/
    cialis canadian pharmacy global pharmacy canada canadian pharmacy king canadian pharmaceuticals online
    canadadrugs.com pharmacy
    cheap prescription drugs online
    compare rx prices
    canadian pharma companies
    mail order pharmacy
    <a href="http://inv4zi0n.blogspot.com/mailto:viiih.dellicious@gmail.com?subject=canada+online+pharmacy+tofs&body=+You+made+your+position+quite+effectively%21.+http%3A%2F%2Fcanadianpharmaciescubarx.com%2F+discount+prescription+drug+canadian+online+pharmacy+canadian+drugs+without+prescription+%5Burl%3Dhttp%3A%2F%2Fcanadianpharmaciescubarx.com%2F%5Dtrust+pharmacy+canada%5B%2Furl%5D+%0D%0Amost+reliable+canadian+pharmacies+%0D%0Acanada+pharmacy+online+reviews+%0D%0Aprescription+drugs+without+doctor+approval+%0D%0Aonline+rx+pharmacy+%0D%0Abuy+prescription+drugs+canada+%0D%0Aking+pharmacy&submit=Enviar”>compare prescription prices

    Reply
  3. QuentinBak

    Your comment is awaiting moderation.

    Nicely put, Thanks a lot!
    http://canadianpharmacyntx.com/
    online pharmacy without a prescription canadian pharmacies that ship to us no prescription pharmacies canadian drugs
    prescription drug
    prescription drug assistance
    prescription drug
    <a href="http://www.ikeaclub.biz.ua/lib/ajax/contact_us_form.php?form_ajax=contacts&name=Quentinnax&email=zittel020eejf%40songwriter.net&phone=86656515968&message=+Beneficial+info.+Thanks%21+http%3A%2F%2Fcanadianpharmacymim.com%2F+online+discount+pharmacy+canadian+pharmacies+compare+prescription+prices+canadian+pharmaceuticals+online+%0D%0Arx+pharmacy+%0D%0Abuy+drugs+online+%0D%0Aonline+pharmacy+canada+%0D%0Amexican+pharmacies+shipping+to+usa+%0D%0Aprescription+pricing+%0D%0Acanadian+meds&security_code=33026224&send=”>mexican pharmacy online
    buying drugs canada
    canada pharmacies prescription drugs

    Reply

Leave a Reply

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