jasonboetcher
asked on
Prompting User for Input; then continuing with code behind
I have the below code. I want to prompt the user for an "OK" or "Cancel" input (like a message box) and then continue with code in my code behind if they select OK. Is this possible? I know I can use a javascript confirm syntax to prompt the user but don't know how to call the code in the code behind from javascript.
Private Sub btnAddRows_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddRows.Click
'TODO: Add prompt to user here; only perform Save routine if they select "OK"
Save()
End Sub
Private Sub btnAddRows_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddRows.Click
'TODO: Add prompt to user here; only perform Save routine if they select "OK"
Save()
End Sub
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
naumann, I can get your to work outside of my main project using a normal asp button, but neither of these solutions are working for me when I use our in house developed button control.
Are there other potential ways to approach this?
Are there other potential ways to approach this?
The ideal solution would be to change the inhouse button control to expose the OnClientCick event.
However, this works just fine:
<span onclick="return confirm('Are you sure?')">
<asp:Button runat="server" Text="Save" onClick="btnAddRows_Click" />
</span>
However, this works just fine:
<span onclick="return confirm('Are you sure?')">
<asp:Button runat="server" Text="Save" onClick="btnAddRows_Click"
</span>
jasonboetcher,
What kind of button you are using in your application? ARe these buttons derived from the Button class or something else?
-Nauman.
What kind of button you are using in your application? ARe these buttons derived from the Button class or something else?
-Nauman.
ASKER
They are custom button controls derived from the standard asp button. I don't know much about them because they weren't created by me and I don't have the source.
You can try this:
<script for="myButton" event="onclick">return confirm('da?');</script>
<asp:Button id="myButton" runat="server" Text="Confirm" OnClick="myClickRoutine" />
<script for="myButton" event="onclick">return confirm('da?');</script>
<asp:Button id="myButton" runat="server" Text="Confirm" OnClick="myClickRoutine" />
Can you add the code I have posted to the button and send me the URL? I would like to see the button HTML output.
-Nauman.
-Nauman.
ASKER
nauman,
It's not a public site so I can't give you a url. Here is the simple sample page HTML that I am using that contains only a button and the code you provided. Hopefully it formats ok and is readable. I did confirm once again that your code works fine with a normal asp button but does not work with our custom button control.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name=vs_defaultClientScrip t content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
<link href="/common/styles/style s.css" type="text/css" rel="stylesheet">
<script language=javascript>
function ConfirmInput()
{
var conf = confirm("Do you want to add a record?");
if (conf == true)
{
return true;
}
else
{
return false;
}
}
</script>
</head>
<body MS_POSITIONING="FlowLayout ">
<form name="Form1" method="post" action="WebForm1.aspx" id="Form1">
<input type="hidden" name="__VIEWSTATE" value="dDwtMTA5ODk5ODgxNzt 0PDtsPGk8M T47PjtsPHQ 8O2w8aTwxP js+O2w8dDx wPDtwPGw8b 25DbGljazs +O2w8amF2Y XNjcmlwdDo gcmV0dXJuI ENvbmZpcm1 JbnB1dCgpX Ds7Pj4+Ozs +Oz4+Oz4+O z4SgpP9bZx brrsqpzuhF RUgf7oVcw= =" />
<SCRIPT LANGUAGE="JavaScript">
function StandardButtonEvent(Event, ControlUniqueID, SBState)
{if (!isNaN(SBState)){document .getElemen tById(Cont rolUniqueI D).State=S BState;}
if (document.getElementById(C ontrolUniq ueID).disa bled) {return false}
switch (Event)
{ case 'DOWN':
if (SBState==0) {return}; var o=document.getElementById( ControlUni queID+'Bor der'); if (o) {o.className='StandardButt on_Down';}
break;
case 'UP':
var o=document.getElementById( ControlUni queID+'Bor der'); if (o) {o.className='StandardButt on_Up';}
break;
case 'CLICK':
try{
var FuncResult=true;
eval('FuncResult='+Control UniqueID+' _Click()') ;
return FuncResult;
} catch (e) {return true;}
break;
}
return true;}
function trapError()
{ alert('Error was trapped');
}
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript">var Button1State=0;</SCRIPT>
<SPAN style="display:inline" State=0 onmousedown="return StandardButtonEvent('DOWN' ,'Button1' ,2);" onmouseenter="StandardButt onEvent('D OWN','Butt on1',Butto n1State);" onmouseleave="StandardButt onEvent('U P','Button 1',Button1 State);" onmouseup="StandardButtonE vent('UP', 'Button1', 0);" onclick="if (StandardButtonEvent('CLIC K','Button 1')){if (typeof(Page_ClientValidat e) == 'function') { if (Page_ClientValidate()==tr ue) {__doPostBack('Button1','' )}} else {__doPostBack('Button1','' )}}" style="cursor:hand;" ID=Button1 class=StandardButton_Butto n><table cellspacing=0 cellpadding=0 ID=Button1Border class=StandardButton_Up style="display:inline"><tr style="row-height:12px;">< td class=StandardButton_Icon> >>&n bsp;</td>< td class=StandardButton_Text nowrap> BUTTON < /td></tr>< /table></s pan>
<input type="hidden" name="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" value="" />
<script language="javascript" type="text/javascript">
<!--
function __doPostBack(eventTarget, eventArgument) {
var theform;
if (window.navigator.appName. toLowerCas e().indexO f("microso ft") > -1) {
theform = document.Form1;
}
else {
theform = document.forms["Form1"];
}
theform.__EVENTTARGET.valu e = eventTarget.split("$").joi n(":");
theform.__EVENTARGUMENT.va lue = eventArgument;
theform.submit();
}
// -->
</script>
<SCRIPT LANGUAGE="JavaScript">docu ment.onerr or=trapErr or;</SCRIP T>
</form>
</body>
</html>
It's not a public site so I can't give you a url. Here is the simple sample page HTML that I am using that contains only a button and the code you provided. Hopefully it formats ok and is readable. I did confirm once again that your code works fine with a normal asp button but does not work with our custom button control.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name=vs_defaultClientScrip
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
<link href="/common/styles/style
<script language=javascript>
function ConfirmInput()
{
var conf = confirm("Do you want to add a record?");
if (conf == true)
{
return true;
}
else
{
return false;
}
}
</script>
</head>
<body MS_POSITIONING="FlowLayout
<form name="Form1" method="post" action="WebForm1.aspx" id="Form1">
<input type="hidden" name="__VIEWSTATE" value="dDwtMTA5ODk5ODgxNzt
<SCRIPT LANGUAGE="JavaScript">
function StandardButtonEvent(Event,
{if (!isNaN(SBState)){document
if (document.getElementById(C
switch (Event)
{ case 'DOWN':
if (SBState==0) {return}; var o=document.getElementById(
break;
case 'UP':
var o=document.getElementById(
break;
case 'CLICK':
try{
var FuncResult=true;
eval('FuncResult='+Control
return FuncResult;
} catch (e) {return true;}
break;
}
return true;}
function trapError()
{ alert('Error was trapped');
}
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript">var Button1State=0;</SCRIPT>
<SPAN style="display:inline" State=0 onmousedown="return StandardButtonEvent('DOWN'
<input type="hidden" name="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" value="" />
<script language="javascript" type="text/javascript">
<!--
function __doPostBack(eventTarget, eventArgument) {
var theform;
if (window.navigator.appName.
theform = document.Form1;
}
else {
theform = document.forms["Form1"];
}
theform.__EVENTTARGET.valu
theform.__EVENTARGUMENT.va
theform.submit();
}
// -->
</script>
<SCRIPT LANGUAGE="JavaScript">docu
</form>
</body>
</html>
Can you paste the output from the following code:
Foreach Control c in Button1.Controls
Response.Write c.GetType()
Next
I think the custom button is a table and we will need to add the attribute to the table row or table cell.
-Nauman.
Foreach Control c in Button1.Controls
Response.Write c.GetType()
Next
I think the custom button is a table and we will need to add the attribute to the table row or table cell.
-Nauman.
Can you override this script?
<SCRIPT LANGUAGE="JavaScript">
function StandardButtonEvent(Event, ControlUniqueID, SBState) {
...
case 'CLICK':
try{
var FuncResult=confirm("ARE YOU SURE?");
eval('FuncResult='+Control UniqueID+' _Click()') ;
return FuncResult;
} catch (e) {return true;}
break;
}
...
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
function StandardButtonEvent(Event,
...
case 'CLICK':
try{
var FuncResult=confirm("ARE YOU SURE?");
eval('FuncResult='+Control
return FuncResult;
} catch (e) {return true;}
break;
}
...
</SCRIPT>
ASKER
I added this code to my page load:
For Each c As Control In Button1.Controls
Response.Write(c.GetType() )
Next
Didn't get anything back; also double checked in debug mode and it doesn't jump inside the for.
I'll see if I can get the javascript override to work.
For Each c As Control In Button1.Controls
Response.Write(c.GetType()
Next
Didn't get anything back; also double checked in debug mode and it doesn't jump inside the for.
I'll see if I can get the javascript override to work.
Your Button1 is not a button. I think it's a label with additional attributes. And there are no controls in it.
Do this: Response.Write(Button1.Get Type()), what the type?
Do this: Response.Write(Button1.Get
ASKER
It just says CommonWebControls.Standard Button when I do the GetType.
ASKER
nauman, I'm still looking into yours; I can't seem to get it to prompt the user yet.