Solved

Message Box in Asp.Net C#

Posted on 2009-04-09
8
554 Views
Last Modified: 2012-08-13
How do I get a message box in Asp.Net C#? At the click of the button I must first get the messag ebox and then by the click of " OK" the Message box must dissapear. Could you explain with an example?
0
Comment
Question by:boneythomas
  • 4
  • 2
8 Comments
 
LVL 23

Expert Comment

by:apresto
ID: 24105617
You can create a javascript function and set the OnclientClick event of the button:
function ShowOkMsg(msg)
{
     alert(msg);
}
Or if you want an "ok/Cancel" message box you can use this:

function ShowOkCancelMsg(msg)
{
     return confirm(msg);
}
and call it like this
<asp:Button runat="server" id="btn" OnClientClick="ShowOkMsg('This is a test');" ... />
0
 
LVL 23

Expert Comment

by:apresto
ID: 24105632
Here is an example i made with a page called Test.aspx:
 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">

    <title>Apresto Test</title>

    <script type="text/javascript" language="javascript">

    function ShowOkMsg(msg)

    {

        alert(msg);

    }

    </script>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:Button runat="server" ID="btnShowMessage" Text="Show Message" OnClientClick="ShowOkMsg('this is a test');" />

    </div>

    </form>

</body>

</html>

Open in new window

0
 
LVL 9

Expert Comment

by:tetorvik
ID: 24105647
I've used ModalPopup from ASP.NET Ajax Control Toolkit. A bit more work to get started, but it provides more options for customizing the messagebox look&feel.
See demonstration:
http://www.asp.net/AJAX/AjaxControlToolkit/Samples/ModalPopup/ModalPopup.aspx
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:boneythomas
ID: 24105732
I need to use it in the aspx.cs page..because it has a condition..only when the condition is succesful, it needs to display this alert message. Can you help me out ?
0
 
LVL 9

Accepted Solution

by:
tetorvik earned 250 total points
ID: 24105778
You can display/hide modalpopup on server-side by using ModalPopup.Show(); and ModalPopup.Hide();
Also client-side methods are available.
0
 
LVL 23

Assisted Solution

by:apresto
apresto earned 250 total points
ID: 24105870
If you just want a simple message box based on a simple condition, then i would say the Ajax solution seems a slight overkill, sorry tetorvik. just my opinion.
If you just want to show an Alert box you code it server side in your .Cs file using this code:
this.Page.ClientScript.RegisterClientScriptBlock
Google it for usage instructions, but they are in the snippet below anyway:

http://blogs.ipona.com/james/archive/2006/10/03/6710.aspx

Open in new window

0
 
LVL 23

Expert Comment

by:apresto
ID: 24105885
However, if you are going to make use of Ajax in other places in your site you would probably benefit from using tetorvik suggestion. Good Luck
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now