Link to home
Start Free TrialLog in
Avatar of jasonboetcher
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
SOLUTION
Avatar of nauman_ahmed
nauman_ahmed
Flag of United States of America image

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
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 jasonboetcher
jasonboetcher

ASKER

svy, I just tried your solution and can't get it to work.  I think it's because we are using an inhouse custom button control.  It prompts me, but if I choose cancel it still runs the code behind button event.

nauman, I'm still looking into yours; I can't seem to get it to prompt the user yet.
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?
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>
jasonboetcher,

What kind of button you are using  in your application? ARe these buttons derived from the Button class or something else?

-Nauman.
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" />
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,

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_defaultClientScript content="JavaScript">
    <meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
      <link href="/common/styles/styles.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="dDwtMTA5ODk5ODgxNzt0PDtsPGk8MT47PjtsPHQ8O2w8aTwxPjs+O2w8dDxwPDtwPGw8b25DbGljazs+O2w8amF2YXNjcmlwdDogcmV0dXJuIENvbmZpcm1JbnB1dCgpXDs7Pj4+Ozs+Oz4+Oz4+Oz4SgpP9bZxbrrsqpzuhFRUgf7oVcw==" />
      <SCRIPT LANGUAGE="JavaScript">
function StandardButtonEvent(Event, ControlUniqueID, SBState)
{if (!isNaN(SBState)){document.getElementById(ControlUniqueID).State=SBState;}
if (document.getElementById(ControlUniqueID).disabled) {return false}
switch (Event)
 { case 'DOWN':
       if (SBState==0) {return}; var o=document.getElementById(ControlUniqueID+'Border'); if (o) {o.className='StandardButton_Down';}
       break;
   case 'UP':
       var o=document.getElementById(ControlUniqueID+'Border'); if (o) {o.className='StandardButton_Up';}
       break;
   case 'CLICK':
       try{
           var FuncResult=true;
           eval('FuncResult='+ControlUniqueID+'_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="StandardButtonEvent('DOWN','Button1',Button1State);" onmouseleave="StandardButtonEvent('UP','Button1',Button1State);" onmouseup="StandardButtonEvent('UP','Button1',0);" onclick="if (StandardButtonEvent('CLICK','Button1')){if (typeof(Page_ClientValidate) == 'function') { if (Page_ClientValidate()==true) {__doPostBack('Button1','')}} else {__doPostBack('Button1','')}}" style="cursor:hand;" ID=Button1 class=StandardButton_Button><table cellspacing=0 cellpadding=0 ID=Button1Border class=StandardButton_Up style="display:inline"><tr style="row-height:12px;"><td class=StandardButton_Icon>&nbsp;>>&nbsp;</td><td class=StandardButton_Text nowrap>&nbsp;BUTTON&nbsp;</td></tr></table></span>
   
<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.toLowerCase().indexOf("microsoft") > -1) {
                  theform = document.Form1;
            }
            else {
                  theform = document.forms["Form1"];
            }
            theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
            theform.__EVENTARGUMENT.value = eventArgument;
            theform.submit();
      }
// -->
</script>
<SCRIPT LANGUAGE="JavaScript">document.onerror=trapError;</SCRIPT>

</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.
Can you override this script?
<SCRIPT LANGUAGE="JavaScript">
function StandardButtonEvent(Event, ControlUniqueID, SBState) {
...
   case 'CLICK':
       try{

           var FuncResult=confirm("ARE YOU SURE?");

           eval('FuncResult='+ControlUniqueID+'_Click()');
           return FuncResult;
          } catch (e) {return true;}
       
       break;
 }
...
</SCRIPT>

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.
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.GetType()), what the type?
It just says CommonWebControls.StandardButton when I do the GetType.
Can you try decompiling the control Dll file?

http://www.remotesoft.com/salamander/

-Nauman.