Embedd_0913
Embedd_0913

Reputation: 16565

How to show a message box in an ASP.NET page?

As I was a Windows programmer it was so easy to show a message box on a form.

But on an ASP.NET page I don't know how can I show it?

Actually I have some condition and based on that I want to show a message box to the user to get his/her response and based on that response I want to continue. For example I want to ask the user "Do you want to continue?" with two buttons "Yes" & "No".

Upvotes: 2

Views: 38798

Answers (8)

Mustafa Iqbal
Mustafa Iqbal

Reputation: 73

i have a solution for you, may be it help you, for using that same message box or conformation dialog of c# in Asp.Net, first you should add namespace,

Using System.Windows.Forms;

then, where you want to call a message box or conformation dialog, you can just call it as simple as in c#, like:

DialogResult dialogResult = MessageBox.Show("Are you shure?", "Some Title", MessageBoxButtons.YesNo);

    if (dialogResult == DialogResult.Yes)
    {
        Response.Redirect("Page.aspx");
    }
    else if (dialogResult == DialogResult.No)
    {
        MessageBox.Show("You Select Nothing to do... :(");
  }

I think, I explained properly, sorry for any mistake....

Upvotes: -2

David Andres
David Andres

Reputation: 31811

You can use the confirm JavaScript function, but that will be limited to OK/Cancel as options. If this is not what you want, you can lean on the VBScript MsgBox client-side function. Bear in mind that doing so will only work with Internet Explorer.

function PerformDelete(id)
{
  if(confirm("I am about to delete this record.  Is this ok?"))
  {
    //your code here
  }
}

Upvotes: 0

Kirtan
Kirtan

Reputation: 21685

You can do this using JavaScript. Include this snippet in your code -

<script type='text/javascript'>
    document.getElementById("someButtonId").onclick = function() {
        var confirmation = window.confirm("Are you sure?"); //confirmation variable will contain true/false.

        if(confirmation) { /* Write code for Yes */ }
        else { /* Write code for No */ }
    }
</script>

Upvotes: 3

Pranali Desai
Pranali Desai

Reputation: 974

window.alert(); window.confirm(); and window.prompt(); This is I guess what you are looking for.

Upvotes: 1

DmitryK
DmitryK

Reputation: 5582

If you REALLY want to have "yes"/"no" buttons (or any buttons that are not your standard OK/Cancel for that matter) you can do the following:

Main page:

<html>
<body>
<script>


function ShowYesNo() {    
    var answer = window.showModalDialog("myModalDialog.htm", '', "dialogWidth:300px; dialogHeight:200px; center:yes");


       document.write('Clicked yes');
    } else {
       document.write('Clicked no');
    }
}

ShowYesNo();

</script>


</body>
</html>

MyModalDialog.htm

<html>
<body>

<p> Do you want to proceed?" </p>
<input type = "button" id = "buttonYes" value = "Yes" onclick = "buttonOnClick('yes')">&nbsp;
<input type = "button" id = "buttonNo" value = "No" onclick = "buttonOnClick('no')">

<script type = "text/javascript">
function buttonOnClick(message) {
    window.returnValue = message;
    window.close();
}
</script> 

</body>
</html>

Upvotes: 0

waqasahmed
waqasahmed

Reputation: 3845

The only way to show a Yes No dialog, is to design a custom one (Javascript confirm can only produce OK and Cancel).

Luckily, ASP.NET Ajax controls (Ajaxcontroltoolkit) makes this job easy, as you can have a panel as your messagebox with the buttons you want, and have a ModalPopupExtender to imitate a dialog.

EDIT:

For what you ask with javascript, you can do it (and it is a much simpler solution than any seen so far), but prepared to only have OK and Cancel as the two possible answers. UI Designer Nightmare ! :(

Basically, have the following two properties in your aspx page for that button or whatever:

onClientClick = "javascript:confirm('you sure you wanna do this?');" onClick="myButton_Click"

onClick will only run if OK is pressed on the msg dialog.

Upvotes: 2

KV Prajapati
KV Prajapati

Reputation: 94653

.aspx markup

<head runat="server">
    <title>Sample Page</title>
    <script type="text/javascript">
      function doSubmit(){
            var confirmation = window.confirm("Are you sure?");
            document.getElementById("HiddenField1")["value"]=confirmation;
            return true;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server" onsubmit="return doSubmit()" >
    <div>
        <asp:HiddenField ID="HiddenField1" runat="server" />
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
    </form>
</body>

Code-behind

protected void Page_Load(object sender, EventArgs e)
{
    if (HiddenField1.Value == "true")
    {

    }
    else
    {

    }
}

Upvotes: 0

rahul
rahul

Reputation: 187110

You can use

window.confirm 

for this.

It displays a modal dialog with a message and two buttons, OK and Cancel.

window.confirm

Eg:

if (window.confirm("Want to see my mood ring?")) 
{ 
    // wants to continue
}
else
{
   // cancel the action
}

Edit:

You can also develop custom message boxes using jQuery. Here is a nice one

jQuery Impromptu

Upvotes: 0

Related Questions