locked
Need to show confirm box before inserting data in database RRS feed

  • Question

  • User-1857293460 posted
    Hi, I am writing a web application using vb.net. I want to show a javascript confirm box before inserting data in database. if user hits cancel subprocedure should exit, otherwise program flow should continue. Some help will be most appreciated !! :)
    Thursday, March 3, 2011 2:20 AM

Answers

All replies

  • User-1971614856 posted

    Let us suppose btnInsert is the button which is Inserting record in database. Write normal code in Click event of btnInsert.

    HTML view of btnInsert should looklike

    <asp:Button runat="server" id="btnInsert" OnClientClick="javascript:if (confirm('Are you sure?')) return true; else return false;"/>

     

    Hope this will help you. Now whenever user will click Cancel then record will not be inserted.

    Thursday, March 3, 2011 6:04 AM
  • User-576912550 posted

    Hi, I am writing a web application using vb.net. I want to show a javascript confirm box before inserting data in database. if user hits cancel subprocedure should exit, otherwise program flow should continue. Some help will be most appreciated !! :)

    Hi

    You can use following code snippest for your insert button

    <asp:button runat="server" id="Insert" OnClientClick="return confirm('Are you sure?');"/>

    Thursday, March 3, 2011 6:32 AM
  • User348806598 posted

    Hi,

    Other than JavaScript confirm, you can also use jQuery Dialog-

    HTML-

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Need to show confirm box before inserting data in database.aspx.cs" 
    EnableEventValidation="false"
    Inherits="Need_to_show_confirm_box_before_inserting_data_in_database" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
        <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.js" type="text/javascript"></script>
        <script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.js" type="text/javascript"></script>
        <link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/themes/redmond/jquery-ui.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript">
            $(document).ready(function() {
                $("input[id*=Button1]").click(function() {
                    $("#dialog-confirm").dialog({
                        modal: true,
                        buttons: {
                            Save: function() {
                                __doPostBack("Button1", "OnClick");
                            },
                            Cancel: function() {
                                //do processing
                                $(this).dialog("close");
                            }
                        }
                    });
                    return false;
                });
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div id="dialog-confirm" style="display:none">Are you sure you want to save?</div>
           Data1: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
           Data2: <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
           Data3: <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
           Data4: <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br />
           Data5: <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox><br />
        <asp:Button ID="Button1" runat="server" Text="Save Data" onclick="Button1_Click" />
        </form>
        
    </body>
    </html>
    Code-

        protected void Page_Load(object sender, EventArgs e)
        {
            ClientScript.GetPostBackEventReference(Button1, "OnClick");
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            //save user data here
        }

    Thursday, March 3, 2011 6:32 AM
  • User-1857293460 posted
    @to all: Thanks for your suggestions. However, I dont want to apply javascript boxes on web page control's events but i want to do something like this . . . If ds.Tables("aTable").Rows.Count = 0 then << Notify user about something . This is where I want to use dialog boxes >> End If
    Friday, March 4, 2011 4:16 AM
  • User1113735518 posted

    Please use OnClientClick event

     

    OnClientClick = "return alert('are you sure?')"

    Friday, March 4, 2011 5:28 AM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 4, 2011 6:50 AM