Tuesday, 17 May 2011

How to Display Yes No message box & handle response in C# ASP.NET

Note:
Please remember javascript is type sensitive & whenever you copy something from internet check the code again for example change the double quotation mark and inverted coma sign.

Normal message box:
Response.Write("<script>alert('Hello')</script>");
Response.Write("<script> alert('" + myStringVariable + "') </script>");

Yes no message box:
Response.Write("<script language=JavaScript>confirm('Are you sure to continue?');</script>");

Yes no response:

. I write the code for .aspx page.
=======================================================================    <asp:Button ID="btnCommit" runat="server" Text="Commit"
        OnClientClick="if(!confirm('Are you quite sure?')) return false;"
        onclick="btnCommit_Click" />
    </asp:Content>
=======================================================================
Copy and paste following code in your code behind(aspx.cs) page.
=======================================================================
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
          Response.Write("<script>alert('Yes button clicked.')</script>");
}
=======================================================================

2nd method:

use javascript in .aspx
<script language="javascript" type="text/javascript">
function confirm() 

if (confirm ==true) 
    do something;
else
     return false;
}
</script>
then write in .aspx.cs
ButtonSave.Attributes.Add("onclick", "return confirm('Are you sure you proceed?');");

No comments:

Post a Comment