?
Solved

how to capture the answer of a javascript confirm function?

Posted on 2005-02-25
11
Medium Priority
?
694 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this 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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

765 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