Show (display) message popup and auto close after some time using C# and VB.Net in ASP.Net

Last Reply one month ago By dharmendr

Posted one month ago

Hello sir

How to show after data insert auto dismissible popup on button click 

Posted one month ago

Hi dilipsharm,

Check this example. Now please take its reference and correct your code.

HTML

<table>
    <tr>
        <td>Name</td>
        <td><asp:TextBox runat="server" ID="txtName" CssClass="form-control" /></td>
    </tr>
    <tr>
        <td>Country</td>
        <td><asp:TextBox runat="server" ID="txtCountry" CssClass="form-control" /></td>
    </tr>
    <tr>
        <td colspan="2" align="center">
            <asp:Button Text="Save" runat="server" OnClick="Save" CssClass="btn-btn-success" />
        </td>
    </tr>
</table>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link type="text/css" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
<style type="text/css">
    .message {
        width: 100%;
        position: absolute;
        top: 10px;
        left: 50%;
        z-index: 100000;
        padding: 0;
        font-size: 15px;
        margin: 0 auto;
    }
</style>
<script type="text/javascript">
    function ShowMessage(message, messagetype) {
        ShowHide(message, messagetype);
        setTimeout(function () {
            $("[id*=alert_div]").hide();
        }, 2000);
    }

    function ShowHide(message, messagetype) {
        var cssclass;
        switch (messagetype) {
            case 'Success':
                cssclass = 'alert-success'
                break;
            case 'Error':
                cssclass = 'alert-danger'
                break;
            case 'Warning':
                cssclass = 'alert-warning'
                break;
            default:
                cssclass = 'alert-info'
        }
        $('body').append('<div id="alert_div" style="margin: 0 10%; -webkit-box-shadow: 3px 4px 6px #999;width:50%" class="alert fade in '
            + cssclass + '"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><strong>'
            + '<span>' + message + '</span></div>');
    }
</script>

Namespaces

C#

using System.Configuration;
using System.Data.SqlClient;

VB.Net

Imports System.Configuration
Imports System.Data.SqlClient

Code

C#

protected void Save(object sender, EventArgs e)
{
    int i = 0;
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    string query = "INSERT INTO Customers VALUES(@Name,@Country)";
    SqlCommand cmd = new SqlCommand(query);
    SqlConnection con = new SqlConnection(conString);

    cmd.Connection = con;
    cmd.Parameters.AddWithValue("@Name", txtName.Text.Trim());
    cmd.Parameters.AddWithValue("@Country", txtCountry.Text.Trim());
    try
    {
        con.Open();
        i = cmd.ExecuteNonQuery();
        con.Close();
        if (i > 0)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "ShowMessage('Record submitted successfully','Success');", true);
        }
    }
    catch (Exception ex)
    {

    }
}

VB.Net

Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
    Dim i As Integer = 0
    Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Dim query As String = "INSERT INTO Customers VALUES(@Name,@Country)"
    Dim cmd As SqlCommand = New SqlCommand(query)
    Dim con As SqlConnection = New SqlConnection(conString)
    cmd.Connection = con
    cmd.Parameters.AddWithValue("@Name", txtName.Text.Trim())
    cmd.Parameters.AddWithValue("@Country", txtCountry.Text.Trim())
    Try
        con.Open()
        i = cmd.ExecuteNonQuery()
        con.Close()

        If i > 0 Then
            ClientScript.RegisterStartupScript(Me.GetType(), "alert", "ShowMessage('Record submitted successfully','Success');", True)
        End If
    Catch ex As Exception
    End Try
End Sub