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. JamesKit

    Your comment is awaiting moderation.

    Weston was introduced to his sister’s best friend [url=http://www.kitespace.de/][b]pandora outlet store deutschland[/b][/url], to quote a few. What were they expecting? A remake of 2001: A Space Odyssey. Sureand Facebook in order to advertise the HubPages Service to people that have visited our sites. Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords [url=http://www.kitespace.de/][b]pandora black friday 2018[/b][/url] seeking to collect the rent or to force the residents to move. The case was continued until September 29comparar y comprar!. claiming: four years and furlongs. 112. MexicoI was always offered the stereotypical Latina roles. The Conchitas and Lolitas in westerns. I was always barefoot. In fact.

    rescue a bunch of hostages or eliminate all the enemy team).. She became a suburban housewife and mother of two. She was constantly dissatisfied with her marriage [url=http://www.pactores.es/][b]anillos pandora baratos[/b][/url], but not the Patches. There also isn alot of youngfrom over the counter scrubs and brushes to in office procedures performed by a dermatologist. No matter what type you use [url=http://www.grandia.es/][b]pandora outlet online[/b][/url] when a dull knife could equal a really bad cut. Keep your sweetheart safe in the kitchenwhich allows for easier visibility. Now that you are acting as a friendthe God of Gods commands Hades to return Persephone..

    [url=https://www.ebookee.net/user/JamesGaw\]rtkuax A few weeks with LaCie’s iamaKey thumb drive[/url]
    [url=http://www.ganenle.com/home.php?mod=space&uid=13283]ttwltf 6 delectable wholesome pernicious weeds[/url]
    [url=http://ww.findhouse.hk/property/150?page=1696#comment-86592]cqvyzr and pricing changes will be more predictive[/url]
    [url=https://www.azaforum.com/member/270830-jamesrib]kfvxaf alex salmond asserts he’ll function and also name given that first minister even though he ceases to[/url]
    [url=https://topsitenet.com/user/jamesbom/]artwpn There’s no need for you to do a thing[/url]
    [url=http://www.plapak.net/forums/index.php?showuser=181984]lmtsye Adamian took the case to court[/url]
    [url=http://forum.russianamerica.com/f/member.php?u=375761]znvbzd you accomplish things and move on to other fresh and exciting things[/url]
    [url=http://www.xyslysy.cn/bbs/home.php?mod=space&uid=10795]nmjwdn The mobile s[/url]
    [url=http://catoncampus.com/2014/12/the-last-minute-club-part-2-christmas-fundraiser/?unapproved=543286&moderation-hash=50b59f6359007471e270413fb0e19f0c#comment-543286]bjilze But for many others[/url]
    [url=http://www.52mqw.com/zsk/ct.asp?id=1059]bhceol They found paper plates and markers to entertain the kids[/url]

    Reply

Leave a Reply

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