Solved

using javascript to product a confirmation box in c# .net

Posted on 2009-04-02
10
877 Views
Last Modified: 2013-12-17
I have a very extensive form that has a checkbox and a Save button at the button.  If the user clicks the Save button, my program just saves info from the form into the database and the record can be opened later for further edits.  If the user clicks the checkbox labeled "Save and Close" then pushes Save button, the program writes the record to the database and it cannot be written to anymore.  Obviously its a good idea to have a Confirmation box pop up that says "You checked the box, this is your last change to confirm you want to do this".   I have read multiple sites suggesting how to put things together in a stringbuilder and passing it to the "onclick" for the button but i can't get this working.

 I am having troubles wrapping my head around how to get this javascript to fire accordingly.  Any help would be really appreciated.

0
Comment
Question by:bwooden
[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
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 9

Expert Comment

by:Gorkem Yuksel
ID: 24053274
Hi,

You can add attributes to any element by using the .Attributes.Add property/method of a control.

I've provided an example below.

Cheers,

G.

// assuming cb is a CheckBox control.
 
cb.Attributes.Add("onclick","Alert('This is an alert box message');");

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24053380
Have you tried something similar to:
<script language="javascript">
    function verify()
    {
        var confirmed = confirm("You checked the box, this is your last change to confirm you want to do this?");
        
        if (confirmed)
        {
            document.your_form_name_here.submit();
        }
    }
</script>
 
<input type="button" id="your_save_button" value="Save" onclick="verify();" />

Open in new window

0
 
LVL 15

Expert Comment

by:oobayly
ID: 24053398
Instead of use the javascript alert method, you could use the confirm method. If the user presses ok it will return 1 (true). Cancel returns 0 (false).

so, ammending gyuksel's code, you could use this:
cb.Attributes.Add("onclick","return confirm('Press OK if you are sure you want to do this?');");

Open in new window

0
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 

Author Comment

by:bwooden
ID: 24053492
oobayly,
 
your suggestion makes perfect sense but it doesn't check the status of checkbox.checked to see if it is in deed checked before saying "Are you sure"    
I was going down a path of checking "GetElementId" for the status of the checkbox but i can't seem to get that figured out.
can you expand that any further?
Thanks
0
 
LVL 15

Expert Comment

by:oobayly
ID: 24053538
Sorry, I read it as the button was Save & Close.

Yet again modifying someonelses code :-)
<script language="javascript">
    function verify(frm)
    {
        // Assuming the the checkbox is called checkConfirm
        if (checkConfirm.checked){
          return confirm("You checked the box, this is your last change to confirm you want to do this?");
        }
        return true;
    }
</script>
 
<input type="button" id="your_save_button" value="Save" onclick="return verify();" />

Open in new window

0
 
LVL 15

Expert Comment

by:oobayly
ID: 24053548
Sorry, the button element should  look like this, forgot to pass the form
<input type="button" id="your_save_button" value="Save" onclick="return verify(this.form);" />

Open in new window

0
 
LVL 15

Accepted Solution

by:
oobayly earned 250 total points
ID: 24053565
Time to get a coffee, I'm making mistakes all over the place!
<script language="javascript">
    function verify(frm)
    {
        // Assuming the the checkbox is called checkConfirm
        if (frm.checkConfirm.checked){
          return confirm("You checked the box, this is your last change to confirm you want to do this?");
        }
        return true;
    }
</script>
 
<input type="button" id="your_save_button" value="Save" onclick="return verify(this.form);" />

Open in new window

0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 24053584
The correct syntax is getElementById():
            function verify()
            {
            	if (document.getElementById("checkId").checked)
            	{
					var confirmed = confirm("You checked the box, this is your last change to confirm you want to do this?");
 
					if (confirmed)
					{
						document.testForm.submit();
					}
                }
            }

Open in new window

0
 

Author Comment

by:bwooden
ID: 24053863
ok guys.. we are making progress

here is the code i have so far and an explanation of what its doing (or not doing)

The code below will recognize the .checked status of the checkId box and it will fire the confirm box as it should, but it doesn't seem to care either way if OK or Cancel is pushed, it will still continue on with the Button1_Click .  If cancel is pushed i need it to stop right there and not continue on with the Button1_Click stuff because it will proceed to write the record into the locked mode.  This is farther than i have got so far though...
//C# code
 
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Attributes.Add("onclick", "return verify(this.form)");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write("Test stuff!!");
    }
 
}
 
 
//ASP code
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
<script type="text/javascript" language="javascript">
     function verify()
            {
            	if (document.getElementById("checkId").checked)
            	{
    	var confirmed = confirm("You checked the box, this is your last change to confirm you want to do this?");
 
					if (confirmed)
					{
						document.form1.submit();
					}
 
 
					
                }
            
            }
</script>
 
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        <asp:CheckBox ID="checkId" runat="server"  /></div>
    </form>
</body>
</html>

Open in new window

0
 
LVL 15

Expert Comment

by:oobayly
ID: 24053948
As this is being called being called from the submit button, when the confirm dialog returns false, the method still exits, and the submit is processed anyway.

This is why I returned the return value of the confirm dialog, and also used "return verify();" in the onclick handler.

Basically you've got 3 possible situations:
  1. Checkbox is checked, and user pressed ok: return true
  2. Checkbox is checked, and user pressed cancel: return false
  3. Checkbox is not checked: return true
The submit button should only actually submit if the verify() method returns true
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server Serialization error 8 47
Building an object from several other objects 4 39
C# DateTime Help 6 46
Easiest intro into .NET CORE? 2 32
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 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