Click here to Skip to main content
16,020,249 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
How to show javascript alert from code behind and get its response to codebehind


eg:

if (textbox1.text !="")
{
i need to show alert here and need to get its response as x
if( x== ok)
{

}
else

{

}


}
Posted

XML
<script type="text/javascript">
function confirmation() {
    var answer = confirm("do you want to delete?")
    if (answer){
                             document.getElementByID("hdnValue").value='true';
    }
    else{
                 document.getElementByID("hdnValue").value='false';
    }
}
</script>



ScriptManager.RegisterStartupScript( this, typeof(Page),  "confirm", "<script>confirmation();</script>",    false);



now you can access the value of hdnValue hiddenField control value further.
 
Share this answer
 
try this js function:-
C#
function confirmBox() {
   if document.getElementById("<%textbox1.ClientID %>").value != "")
   {
        var ans = confirm('Do you want to save this data?');
                  if (ans == true) {
                      return true;
                  }
                  else {
                      return false;
                  }
  }
}


refer:-
http://www.aspsnippets.com/Green/Articles/Server-Side-Code-Behind-Yes-No-Confirmation-Message-Box-in-ASPNet.aspx[^]
 
Share this answer
 
v2
C#
ScriptManager.RegisterStartupScript(this,this.GetType(),"redirect",
"alert('test 9'); window.location='" +
Request.ApplicationPath + "anotherpage.aspx';",true);
 
Share this answer
 
Comments
fiashieen 17-Feb-14 7:27am    
this answer is not meeting my requirement for me because i want to know (the user clicked yes or no button)
use

VB
ScriptManager.RegisterStartupScript(
        this,
        typeof(Page),
        "Alert",
        "<script>alert('" + sMessage + "');</script>",
        false);
 
Share this answer
 
Comments
fiashieen 17-Feb-14 7:27am    
this answer is not meeting my requirement for me because i want to know (the user clicked yes or no button)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900