Thursday, 6 November 2014

Show Alert Message in Asp.net from Code behind in C#

Here I am explain how to show alert message in Asp.Net  from code behind using C#.

Source Code :  

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>show alert message in asp.net from code behind Asp.NetJitendra</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnClick" Text="Click" runat="server" OnClick="btnClick_Click" />
    </div>
    </form>
</body>
</html>

Code Behind :

Source 1:- 

protected void btnClick_Click(object sender, EventArgs e)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "alertmsg", "alert('This alert msg from code behind');", true);
    }


Source 2:- 

protected void btnClick_Click(object sender, EventArgs e)
    {
       Messagebox("This alert msg from code behind");
    }
    private void Messagebox(string Message)
    {
        Label lblMessageBox = new Label();

        lblMessageBox.Text =
            "<script language='javascript'>" + Environment.NewLine +
            "window.alert('" + Message + "')</script>";

        Page.Controls.Add(lblMessageBox);

    }


Output :-

         

No comments:

Post a Comment