Message Box in Asp.Net C#

Posted on 2009-04-09
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?
Question by:boneythomas
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
LVL 23

Expert Comment

ID: 24105617
You can create a javascript function and set the OnclientClick event of the button:
function ShowOkMsg(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');" ... />
LVL 23

Expert Comment

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" "">
<html xmlns="" >
<head id="Head1" runat="server">
    <title>Apresto Test</title>
    <script type="text/javascript" language="javascript">
    function ShowOkMsg(msg)
    <form id="form1" runat="server">
        <asp:Button runat="server" ID="btnShowMessage" Text="Show Message" OnClientClick="ShowOkMsg('this is a test');" />

Open in new window


Expert Comment

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:
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

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 ?

Accepted Solution

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.
LVL 23

Assisted Solution

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:
Google it for usage instructions, but they are in the snippet below anyway:

Open in new window

LVL 23

Expert Comment

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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

749 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