Subscribe to Subscribers
Talk to Webmaster Tony John


Resources » .NET programming » .NET Framework

Show Alert Box using ASP.NET


Posted Date:     Category: .NET Framework    
Author: Member Level: Gold    Points: 4


This article demonstrates how to display a client side messagebox using ASP.NET webforms and javascript.



 


We can show alert message using asp.net. For that we have to register a ClientScriptBlock and invoke this option.

Sample Code


< %@ Page Language="C#" %>
< script runat="server">
void AlertBtn_Click(object sender, EventArgs e)
{
string showmessage = "Test Message Box";
string alertScript = "< script language=JavaScript> ";
alertScript += "alert('" +showmessage +"');";
alertScript += "< /script" +"> ";

if (!IsClientScriptBlockRegistered("alert"))
this.RegisterClientScriptBlock("alert", alertScript);
}
< /script>
< html>
< head>
< /head>
< body>
< form runat="server">
< asp:Button id="btn" onclick="AlertBtn_Click"
runat="server" Text="Button"> < /asp:Button>
< /form>
< /body>
< /html>



In the sample we can see the alert message on Click of "btn"- button click.





Did you like this resource? Share it with your friends and show your love!


Responses to "Show Alert Box using ASP.NET"
Author: Alwin Durairaj    06 Jul 2004Member Level: Bronze   Points : 0
Hi I tried ur logic, but it does not work well.

Whenever i execute this code it diplays the script language in browsers window. Instead of displaying message box.

Could u help me to display the message box using ur logic.

I have attached my html part and code behind given below

aspx file

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="my_web_al2.WebForm1" %>





WebForm1













code behind file

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace my_web_al2
{
///
/// Summary description for WebForm1.
///

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}



#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}


Hope u do ur best as soon as possible.
with good cheers,
Alwin



Author: Siddarth Tamagond    14 Apr 2005Member Level: Bronze   Points : 0
Hi Alwin Durairaj,
The code is correct , It will work fine, if u just remove the space while opening the tags and closing the tags, or just copy & paste the below code, I removed it.

<%@ Page Language="C#" %>


(14 Apr 2005)



Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Search TreeNode
    Previous Resource: Insert data into MS Access database using OledbCommand in VB.net
    Return to Resources
    Post New Resource
    Category: .NET Framework


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    (No tags found.)



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.