Results 1 to 10 of 10

Thread: [RESOLVED] Embed or use javascript confirmation box in asp.net save button

  1. #1

    Thread Starter
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,024

    Resolved [RESOLVED] Embed or use javascript confirmation box in asp.net save button

    Hi,

    In javascript there is an alert message box...What i want to do is include a javascript popup/ for confirmation of whether the user will save the record or not..

    Since asp.net has no message box for web, i was thinking of using javascript..


    heres the scenario:

    asp.net 2.0 code Code:
    1. protected void saveEmployeeRecord_click()
    2.   {
    3.  
    4.          try
    5.          {
    6.          objEmp.Emp_ID = this.txtEmployee.Text;
    7.          //other statements for assigning to object
    8.  
    9.         //show the popup dialog here
    10.        //if you click ok in the popup
    11.        //call the save method to the database
    12.        //if you click cancel, do not save the record
    13.        
    14.          }
    15.          catch(Exception ex)
    16.          {
    17.               //do error handling here
    18.          }
    19. }

    or what are some other means to do this?

    Greg
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  2. #2
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: Embed or use javascript confirmation box in asp.net save button


  3. #3

    Thread Starter
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,024

    Re: Embed or use javascript confirmation box in asp.net save button

    thanks for the input on using ajax

    however some of the projects does not use ajax for some reasons....

    But there are some that use ajax...

    Is my question on post #1 doable?

    Greg
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  4. #4
    Fanatic Member
    Join Date
    Jun 2004
    Location
    All useless places
    Posts
    917

    Re: Embed or use javascript confirmation box in asp.net save button

    If you're not using AJAX then JavaScript is the way to go. for that you'll have to add the JS method to the button attributes
    Code:
    saveEmployeeRecord.Attributes.Add("onclick","javascript:ConfirmUpdate()")
    And then in the JS you can do something like
    Code:
    function ConfirmUpdate()
            {
                 var response = confirm("Are you sure?");
                if (response)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Embed or use javascript confirmation box in asp.net save button

    Dude.

    return confirm('Are you sure?');

    You can use ButtonName.Attributes.Add() to add the javascript to the button's onclick.

  6. #6
    Fanatic Member
    Join Date
    Jun 2004
    Location
    All useless places
    Posts
    917

    Re: Embed or use javascript confirmation box in asp.net save button

    Yes a more brief version but that is the way I do as I generally try to avoid writing any inline JS as much as possible. No reasons, just preference.

  7. #7

    Thread Starter
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,024

    Re: Embed or use javascript confirmation box in asp.net save button

    tnx for the code:

    here's the running one...actually i used hidden fields for this:

    Code:
    <script type ="text/javascript" language="javascript">
        
            //function to save and update
            function ConfirmSave()
            {
                //       return confirm("Are you sure you want to update?");            
                //       document.getElementById("HiddenField1").value = "3434";
                //            var a;
                //            a = return confirm("Are you sure you want to update?");   
                //            document.getElementById("HiddenField1").value = a;         
                
                var response = confirm("Are you sure you want to save?");
                if (response)
                {
                    //return for save and update;
                    document.getElementById("HiddenField1").value = "true";         
                }
                else
                {
                   // return for cancel;
                   document.getElementById("HiddenField1").value = "false";         
                }
            } 
         
    </script>
    Code:
    protected void Page_Load(object sender, EventArgs e)
        {
            //use this for deletion of other programs
            this.btnSave.Attributes.Add("onclick", "return ConfirmSave();");  //this will trigger if the btnSave will clicked
    
         }
    here's the code for save:

    code Code:
    1. protected void btnSave_click()
    2. {
    3.   //use this code to test whether to save or not      
    4.   string sq = this.HiddenField1.Value;
    5.  
    6.   if(sq ==  "true")
    7.  {
    8.      //save statement
    9.      //insert into and etc......
    10.  }
    11.          
    12. }


    Tnx guys

    Greg
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Embed or use javascript confirmation box in asp.net save button

    Quote Originally Posted by rjv_rnjn
    Yes a more brief version but that is the way I do as I generally try to avoid writing any inline JS as much as possible. No reasons, just preference.
    When you call the ConfirmUpdate() method, that's still inline javascript. There is no difference.

  9. #9
    Fanatic Member
    Join Date
    Jun 2004
    Location
    All useless places
    Posts
    917

    Re: Embed or use javascript confirmation box in asp.net save button

    When you call the ConfirmUpdate() method, that's still inline javascript. There is no difference.
    I'm not sure I get you. But I never suggested calling JS methods from my .Net code. I just add the method name to button attributes and that is pure .Net code.

  10. #10
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] Embed or use javascript confirmation box in asp.net save button

    I didn't suggest that either. You mentioned that you want to avoid writing inline JS. Calling a javascript function is inline javascript.

    Doing an .Attributes.Add("onclick","javascript:ConfirmUpdate()") is also the equivalent of using inline js because that's what it renders out to in the <input> tag's attribute.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width