• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 380
  • Last Modified:

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
0
jasonboetcher
Asked:
jasonboetcher
  • 6
  • 5
  • 5
2 Solutions
 
nauman_ahmedCommented:
In the Page_Load event add the following:

MyAddButton.Attributes.Add("onClick","javascript: return ConfirmInput();")

In ASPX add the following:

<script language=javascript>
function ConfirmInput()
{
   var conf = confirm("Do you want to add a record?");
   if (conf == true)
   {
         return true;
   }
  else
  {
   return false;
  }
}
</script>

-Nauman.
0
 
svyCommented:
2.0:
<asp:Button runat="server" OnClientClick="return confirm('Are you sure?');" Text="Save" onClick="btnAddRows_Click" />

1.1:
<span onclick="return confirm('Are you sure?')">
<asp:Button runat="server" Text="Save" onClick="btnAddRows_Click" />
</span>
0
 
jasonboetcherAuthor Commented:
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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
jasonboetcherAuthor Commented:
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?
0
 
svyCommented:
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>
0
 
nauman_ahmedCommented:
jasonboetcher,

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

-Nauman.
0
 
jasonboetcherAuthor Commented:
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.
0
 
svyCommented:
You can try this:
<script for="myButton" event="onclick">return confirm('da?');</script>
<asp:Button id="myButton" runat="server" Text="Confirm" OnClick="myClickRoutine" />
0
 
nauman_ahmedCommented:
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.
0
 
jasonboetcherAuthor Commented:
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>
0
 
nauman_ahmedCommented:
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.
0
 
svyCommented:
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>

0
 
jasonboetcherAuthor Commented:
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.
0
 
svyCommented:
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?
0
 
jasonboetcherAuthor Commented:
It just says CommonWebControls.StandardButton when I do the GetType.
0
 
nauman_ahmedCommented:
Can you try decompiling the control Dll file?

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

-Nauman.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now