Link to home
Start Free TrialLog in
Avatar of boneythomas
boneythomas

asked on

Message Box in Asp.Net C#

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?
Avatar of apresto
apresto
Flag of Italy image

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');" ... />
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

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
Avatar of boneythomas
boneythomas

ASKER

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 ?
ASKER CERTIFIED SOLUTION
Avatar of tetorvik
tetorvik
Flag of Finland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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