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...
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...
<SCRIPT language=javascript>
var doc = document.forms[0];
if (doc.lbHistory.value != "")
{
if confirm('Delete ' + doc.lbHistory.value + '?') {
document.forms[0].submit() ;
}
}
</SCRIPT>
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].lbHisto ry.value != '') return confirm('Delete ' + document.forms[0].lbHistor y.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
btnDelete.Attributes.Add("
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>
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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();")
1. remove onclick from ur tage
2. in page_load add this code
btnRemove.Attributes.Add("
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....
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
on a final note i slighly modified the btn.attributes.add line to the following:
btnRemove.Attributes.Add(" onclick"," javascript : return test();")
btnRemove.Attributes.Add("
ASKER