Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

how to capture the answer of a javascript confirm function?

Posted on 2005-02-25
11
Medium Priority
?
709 Views
Last Modified: 2008-02-01
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...



0
Comment
Question by:brdrok
11 Comments
 
LVL 7

Author Comment

by:brdrok
ID: 13407411
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.

0
 
LVL 10

Expert Comment

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

     }
</SCRIPT>
0
 
LVL 18

Expert Comment

by:tusharashah
ID: 13407544
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:kollu
ID: 13407571
<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>

0
 
LVL 1

Assisted Solution

by:khareatul
khareatul earned 400 total points
ID: 13409080
Hi,
      If u want the ServerSide button click event should call when user click on 'Yes' in confirm box then try this:
     
<script language='javascript'>
   function CofirmDelete()
    {
                               if(confirm("Are you sure you want to delete !"))
            {
                  window.event.returnValue = true;
            }
                                else
                                {
                                           return false;
                                }
                             
    }
</script>

            When u'll click on Yes the only button click event will execute(rather then postback the whole page)


   
0
 
LVL 7

Author Comment

by:brdrok
ID: 13420405
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

0
 
LVL 10

Expert Comment

by:NetDeveloper
ID: 13420620
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();")
0
 
LVL 7

Author Comment

by:brdrok
ID: 13421703
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....
0
 
LVL 10

Accepted Solution

by:
NetDeveloper earned 1600 total points
ID: 13421908
put return false;

or

it is not work

window.event.returnValue = false;


          else
          {
               doc.lblDelete.value = "not deleting";
               alert('denied');
               return false;
          }


lemmenow
0
 
LVL 7

Author Comment

by:brdrok
ID: 13422136
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.  
0
 
LVL 7

Author Comment

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

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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question