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”.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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>

Method 2

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.

Method 3

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

Method 4

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

Method 5

.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
    {

    }
}

Method 6

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>

Method 7

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
  }
}

Method 8

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….


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x