Wednesday, 22 May 2013

Yes/No Message Box ASP.NET


Yes/No Message Box ASP.NET 


You might have got into this situation of displaying a message box to confirm the occurrence of  an event similar to the Yes/No message box in Windows Forms. To achieve the same functionality in asp.net that too dynamically you need few JavaScript codes and  you would be able to achieve the same easily..

Design Part:


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Confirm Yes No Demo</title>
        <script type="text/javascript" language="javascript">
        function ConfirmYesNo()
        {
          if (confirm("Are you sure?")==true)
          {
            return true;
          }
          else
          {
            document.getElementById('Label1').innerHTML = '';
            alert("You have Cancelled the Operation.");
            return false;
          }
        }
        </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="Button1" runat="server" Text="Click Me" onclick="Button1_Click" OnClientClick="return ConfirmYesNo();" />
    <br />
    <br />
        <asp:Label ID="Label1" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>

Code-Behind:


    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "";
        Label1.Text = "Welcome to Dotnetvishal.com";
    }

Demo:



back to top