jQuery Latest CDN Links

Show Javascript Alert Message in ASP.Net from Server Side using C# and VB.Net


Show Javascript Alert Message in ASP.Net from Server Side using C# and VB.Net


In this article I will explain how to show javascript alert popup in Asp.Net from the server side using C# and VB.Net. Here you can show the javascript alert popup in different events on page load and button submit.

Show message when users visit the page:

In the Page Loading event, the following JavaScript function will be recorded using the RegisterClientScriptBlock function. The below javascript function display a greetings message to the user in a javascript alert popup. It will show the message to the user when a user opens the page.

C#

protected void Page_Load(object sender, EventArgs e)
        {
            string message = "Hello! WebcodeSpider.";
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("<script type = 'text/javascript'>");
            sb.Append("window.onload=function(){");
            sb.Append("alert('");
            sb.Append(message);
            sb.Append("')};");
            sb.Append("</script>");
            ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
        }

VB.Net

Protected  Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            Dim message As String =  "Hello! WebcodeSpider." 
            Dim sb As System.Text.StringBuilder =  New System.Text.StringBuilder() 
            sb.Append("<script type = 'text/javascript'>")
            sb.Append("window.onload=function(){")
            sb.Append("alert('")
            sb.Append(message)
            sb.Append("')};")
            sb.Append("</script>")
            ClientScript.RegisterClientScriptBlock(Me.GetType(), "alert", sb.ToString())
End Sub

Show message when users submit the page:

In the Page Loading event, the following JavaScript function will be registered using the RegisterOnSubmitStatement function. The below javascript function display a greetings message  to the user in a javascript alert popup. It will show the message to the user when a user submits the page.

C#

protected void Page_Load(object sender, EventArgs e)
        {
            string message = "Your request is being processed.";
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("alert('");
            sb.Append(message);
            sb.Append("');");
            ClientScript.RegisterOnSubmitStatement(this.GetType(), "alert", sb.ToString());
        }

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    Dim message = "Your request is being processed."
    Dim sb As StringBuilder = New StringBuilder()
    sb.Append("alert('")
    sb.Append(message)
    sb.Append("');")
    ClientScript.RegisterOnSubmitStatement([GetType](), "alert", sb.ToString())

End Sub

Display Message Box on Button Click

In the button click event, the following JavaScript function will be registered using the RegisterClientScriptBlock function. The below javascript function display a greetings message  to the user in a javascript alert popup. It will show the message to the user when a user click the button.

C#

protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string message = "Order Placed Successfully.";
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("<script type = 'text/javascript'>");
            sb.Append("window.onload=function(){");
            sb.Append("alert('");
            sb.Append(message);
            sb.Append("')};");
            sb.Append("</script>");
            ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
        }

VB.Net

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim message = "Order Placed Successfully."
    Dim sb As StringBuilder = New StringBuilder()
    sb.Append("<script type = 'text/javascript'>")
    sb.Append("window.onload=function(){")
    sb.Append("alert('")
    sb.Append(message)
    sb.Append("')};")
    sb.Append("</script>")
    ClientScript.RegisterClientScriptBlock([GetType](), "alert", sb.ToString())
End Sub

Please mail me at hemanta@webcodespider.net if you want any code solution from me. 

Thank You 😊 

Implement Google Places (Address) AutoComplete TextBox in ASP.Net using C# and VB.Net

Comments