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

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

But in an ASP.NET page I don't know how to show this?

Actually I have some kind of condition and based on that I want to show a message box to the user to get his response and based on this response I want to continue. For example, I want to ask the user "Continue?" with two buttons "Yes" and "No".

+2


source to share


8 answers


The only way to show a Yes No dialog is to create a custom one (Javascript confirmation can only be done with OK and Cancel).

Fortunately, ASP.NET Ajax Controls (Ajaxcontroltoolkit) make this easier because you can have a pane as your window with the buttons you want and have a modalPopupExtender to simulate a dialog.

EDIT:

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



Basically your aspx page has something different for this button:

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

      

onClick will only fire if OK is clicked in the msg dialog.

+2


source


You can do this using JavaScript . Include this piece of 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>

      

+3


source


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

+1


source


you can use

window.confirm 

      

for this.

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

window.confirm

For example:

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

      

Edit:

You can also create custom message boxes using jQuery. Here is a good

jQuery Impromptu

0


source


You can use the JavaScript validation function , but this will be limited to the OK / Cancel parameters as parameters. If this is not what you want, you can rely on the VBScript MsgBox client function. Be aware that this will only work with Internet Explorer.

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

      

0


source


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

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

    }
    else
    {

    }
}

      

0


source


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

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

      

0


source


I have a solution for you, maybe it will help you, to use the same message box or C # conformation dialog in Asp.Net, you must add the namespace first,

Using System.Windows.Forms;

then when you want to invoke a message dialog or a configuration dialog, you can just call it as simple as in C #, for example:

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 correctly, sorry for any mistake ....

-1


source







All Articles