Link to home
Start Free TrialLog in
Avatar of brdrok
brdrok

asked on

how to capture the answer of a javascript confirm function?

Hello,

a javaScript newbie here.  I have written a little javascript function like so inside the html section.

<SCRIPT language=javascript>
      var doc = document.forms[0];
      
      if (doc.lbHistory.value != "")
      {
            confirm('Delete ' + doc.lbHistory.value + '?');
      }
</SCRIPT>

I got the confirm message box to appear just fine, but how do I capture the user's answer (yes/no) with asp.net?

thanks...



Avatar of brdrok
brdrok

ASKER

ohh btw.  this javascript function is called when the user clicks a button.  what would be great if it somehow would be possible to only postBack to the server if the user clicks "yes" on the messageBox.

<SCRIPT language=javascript>
     var doc = document.forms[0];
     
     if (doc.lbHistory.value != "")
     {
          if confirm('Delete ' + doc.lbHistory.value + '?') {
             document.forms[0].submit();
         }

     }
</SCRIPT>
You can write your function in CodeBehind like following:

btnDelete.Attributes.Add("OnClick", "if (document.forms[0].lbHistory.value != '') return confirm('Delete ' + document.forms[0].lbHistory.value + '?'); " );


or Modify your JavaScript like following:
<SCRIPT language=javascript>
     var doc = document.forms[0];
     
     if (doc.lbHistory.value != '')          // use ' instead of "
     {
          return confirm('Delete ' + doc.lbHistory.value + '?');   // this will return false or true based on users answer
     }
</SCRIPT>


-tushar
<SCRIPT language=javascript>
     var doc = document.forms[0];
     
     if (doc.lbHistory.value != "")
     {
          if confirm('Delete ' + doc.lbHistory.value + '?') //true i.e for yes
         {
             document.forms[0].submit();
         }
      else
        {
           return false;
       }

     }
</SCRIPT>

SOLUTION
Avatar of khareatul
khareatul

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of brdrok

ASKER

sorry for responding so late.  do not have internet connection @ home :)

thanks for all the input...

I tried to wrap it into a function and have that function being called but keep getting an error something to the effect of:
External component has thrown an exception

Below is what I have been trying to do:

<SCRIPT language=javascript>
function test()
{
      var doc = document.forms[0];
      
      if (doc.lbHistory.value != "")
      {
            if (confirm('Delete ' + doc.lbHistory.value + '?')==true)
            {
                  alert('Deleting...');
            }
            else
            {
                  alert('denied');
            }
      }
}
</SCRIPT>

below is the html section of the button that is suppose to call the function but i get the following

      <asp:button id=btnRemove
            style="Z-INDEX: 151; LEFT: 504px; POSITION: absolute; TOP: 368px"
            runat="server" Width="80px" Text="Remove" ForeColor="White"
            BackColor="DarkKhaki"  onclick="test()" / >

thanks

Hi, this is not perfectly correct..

1. remove onclick from ur tage

2. in page_load add this code

btnRemove.Attributes.Add("onclick","javascript:test();")
Avatar of brdrok

ASKER

thanks @ NetDev.

i got the error to disappear.  however, it seems like it's still posting back to the server after clicking cancel.

sorry to keep adding to this question but this whole idea of javascript + asp.net is still very foreign to me (note: increased the points a little)

in an effort to deepen my knowledge of javascript i added the following:

<SCRIPT language=javascript>
function test()
{
      var doc = document.forms[0];
      
      if (doc.lbHistory.value != "")
      {
            if (confirm('Delete ' + doc.lbHistory.value + '?')==true)
            {
                  alert('Deleting...');
                  doc.lblDelete.value = "deleting";  <---is a label i recently added.  it's a web server control
                   window.event.returnValue = true;
            }
            else
            {
                  doc.lblDelete.value = "not deleting";
                  alert('denied');
            }
      }
}
</SCRIPT>

Aside from the page posting back even after clicking on "cancel", the "alert('denied') never fires and i am not able to add some text into my label control called "lblDelete".

thanks for all the help so far....
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of brdrok

ASKER

thanks NetDev...

works now.  even when clicking on cancel, the browswer does NOT post back to the servcer so that is great.  not sure how or why but it works not.   i swear...sometimes i think this whole computer thingie runs on black magic.  
Avatar of brdrok

ASKER

on a final note i slighly modified the btn.attributes.add line to the following:

btnRemove.Attributes.Add("onclick","javascript: return test();")