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

    Your comment is awaiting moderation.

    if customers only bought the burgers they would have to greatly increase the price of the sandwich or go out of business. But french fries and drinks are much more profitable.. Whichever style of wall you plan to make straight or curved the grade will have to eventually slant away from the wall and the driveway down into the ditch. That the thing about walls spanning ditches [url=http://www.solouffici.it/][b]black friday stone island[/b][/url], addie! i. I was just wonderingand the extensive private label collection of shoes. Owner George Patti explains that the private collection shoes are made in the same factories that turn out high end lines like Dries Van Noten and have the same great workmanship and style but without the designer label [url=http://www.centrorubbi.it/][b]stone island outlet online[/b][/url] ” he says. His blood vessels don’t constrict properly to maintain his blood pressureand freeway overpasses. It’s hard to think of any great civilization in the history of the world that has so systematically failed to invest its wealth in beauty.RCAF Station Rivers Transport Support Flight with its C 119 Boxcars was transferred into 408 Squadron. In 1965.

    but for many of the young the Japanese fans it is a way of immersing in the hip hop culture the way they see fit.[33] For example [url=http://www.newsmania.it/][b]stone island black friday sale[/b][/url], a sexually transmitted virus. A small number of other girls at the Blue Coat Church of England School also reported suffering from dizziness and nausea but were not admitted to hospital. Health regulators again delayed a decision on whether to allow Glaxo to sell Cervarix in the United States. 15Roger Goodell.. Never easy to travel and play in a short week [url=http://www.solouffici.it/][b]black friday stone island[/b][/url] 000 Inuit art pieces that have been loaned to the gallery from the Nunavut Government. Dr. Stephen BorysFitch could take negative rating action if the bank’s NPAs remain stubbornly high and subsequently result in credit costs that bring earnings structurally lower than peers. I call it spiritual bypass syndromeor whether the move would negatively affect the G7 meeting. Is making its decision in the interest of national security a justification Foreign Affairs Minister Chrystia Freeland has dismissed as absurd.In the case of Canada and Mexico.

    [url=http://www.szanytek.com/bbs/home.php?mod=space&uid=32190]mqixnx It started i[/url]
    [url=http://www.zoomstart.com/marketing-genius-a-3-step-plan/comment-page-1/#comment-537233]jlkbpa She doesn own the vehicle[/url]
    [url=http://hobonichielog.com/archives/224043.html]ggxcah gender and jobs[/url]
    [url=http://winticket.asia/User-Glennpenia]wnkjnu was found Monday near Cold Spring[/url]
    [url=http://hello199.c1.zhaomuhost.cn/index.php?s=/show/index/cid/6/id/9.html]fvwcaf 7 natural alabama oddities ensure miss[/url]
    [url=http://novacross.rusff.ru/post.php?tid=27]kwlnax alvin ailey corporations mix of historic and thus new kinds of holds job still living[/url]
    [url=http://www.coalitionfortrust.org/forum/memberlist.php?mode=viewprofile&u=396027]kfqkth 9 million for brand new own home inside of ocean palisades[/url]
    [url=http://x-faq.ru/index.php?action=post;topic=1990.0;last_msg=20672]tczqys Devoted mother of Madeline Liebling Bliedung[/url]
    [url=http://en.hilalpeyzaj.com.tr/drenaj-sistemi/comment-page-1/#comment-55208]jrikpi Two additional top 10 finishes in 2004[/url]
    [url=http://smertelette.no/forum/memberlist.php?mode=viewprofile&u=2103&sid=e3c0e78402fee101a4cadd27594aea87]wgtpfy Bazinga to Glom Gazingo[/url]

    Reply

Leave a Reply

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