Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a Javascript confirmation popup correctly 1

Status
Not open for further replies.

ajclifford

Programmer
Jan 16, 2005
17
AU
Hello,

I am trying to use Javascript within an ASP.NET web page to create an alert confirmation popup. A certain function run if a user clicks Yes or No to the alert popup. I have tried the following code so far.

The HTML code is as follows:
Code:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="manage.aspx.vb" Inherits="Contractors.manage"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"[/URL] />
<html lang="en">
 <head>
  <title>Contractors and Consultants Induction Search Tool</title>
  <meta http-equiv="content-type" content="text/html;charset=utf-8">
  <meta http-equiv="Content-Style-Type" content="text/css">
  <link rel="stylesheet" href="style.css" type="text/css">
  <script language="Javascript">
  function deleteperson() {
  var x = confirm('Are you sure you would like to delete this person?');
  if (x == true) {
  alert('You clicked yes');
  }
  else {
  alert('You clicked cancel, goodbye');
  }
  }
  </script>
 </head>
 <body>
  <form id="form1" method="post" runat="server">
   <table id="table1" cellspacing="0" cellpadding="0" border="0">
    <tbody>
     <tr>
      <td width="20" background="images/topleft_corner.gif" height="20"></td>
      <td background="images/top_border.gif" height="20"></td>
      <td width="20" background="images/topright_corner.gif" height="20"></td>
     </tr>
     <tr>
      <td width="20" background="images/left_border.gif"></td>
      <td valign="top" bgcolor="#f5f5f5"><span class="title">
       Contractors and Consultants Database Search</span><br />
       <br />
       <asp:linkbutton id="lnkAdd" runat="server" text="Add"></asp:linkbutton> -
       <asp:linkbutton id="lnkEdit" runat="server" text="Edit"></asp:linkbutton> -
       <asp:linkbutton id="lnkDelete" runat="server" text="Delete"></asp:linkbutton> -
       <asp:linkbutton id="lnkLogout" runat="server" text="Logout"></asp:linkbutton><br />
       <br />

       <!-- Add Panel -->
       <asp:panel id="pnlAdd" runat="server" visible="false">
        <table id="table2" cellpadding="10" width="600" align="center">
         <tr>
          <td colspan="2" valign="top">
           <p>To add a new person, please fill in the form below.
           </p>
          </td>
         </tr>
         <tr>
          <td class="info" valign="top">
           <table id="Table3" cellpadding="10">
            <tr>
             <td class="heading2">First Name:</td>
             <td>
              <asp:textbox id="txtAddFirstName" runat="server"></asp:textbox></td>
            </tr>
            <tr>
             <td class="heading2">Surname:</td>
             <td>
              <asp:textbox id="txtAddSurname" runat="server"></asp:textbox></td>
            </tr>
            <tr>
             <td class="heading2">Address:</td>
             <td>
              <asp:textbox id="txtAddAddress" runat="server"></asp:textbox></td>
            </tr>
            <tr>
             <td class="heading2">Contact Number:</td>
             <td>
              <asp:textbox id="txtAddContactNumber" runat="server"></asp:textbox></td>
            </tr>
            <tr>
             <td class="heading2">Vendor Number:</td>
             <td>
              <asp:textbox id="txtAddVendorNumber" runat="server"></asp:textbox></td>
            </tr>
            <tr>
             <td class="heading2">Date Issued:</td>
             <td>
              <asp:textbox id="txtAddDateIssued" runat="server"></asp:textbox></td>
            </tr>
            <tr>
             <td colspan="2">
              <asp:Button id="cmdAdd" runat="server" Text="Add"></asp:Button></td>
            </tr>
           </table>
          </td>
         </tr>
        </table>
       </asp:panel>

       <!-- Edit Panel -->
       <asp:panel id="pnlEdit" runat="server" visible="false">
        <table id="table4" cellpadding="10" width="600" align="center">
         <tr>
          <td colspan="2" valign="top">
           <p>To edit a person's details, please select them from the listbox.
           </p>
          </td>
         </tr>
         <tr>
          <td class="info" valign="top">
           <asp:listbox id="lstEdit" runat="server" Rows="12"></asp:listbox>&nbsp;
           <p>
            <asp:button id="cmdSelectPerson" runat="server" Text="Edit"></asp:button></p>
          </td>
          <td class="info" valign="top">
           <table id="table5" cellpadding="10">
            <tr>
             <td class="heading2">First Name:</td>
             <td>
              <asp:textbox id="txtEditFirstName" runat="server"></asp:textbox></td>
            </tr>
            <tr>
             <td class="heading2">Surname:</td>
             <td>
              <asp:textbox id="txtEditSurname" runat="server"></asp:textbox></td>
            </tr>
            <tr>
             <td class="heading2">Address:</td>
             <td>
              <asp:textbox id="txtEditAddress" runat="server"></asp:textbox></td>
            </tr>
            <tr>
             <td class="heading2">Contact Number:</td>
             <td>
              <asp:textbox id="txtEditContactNumber" runat="server"></asp:textbox></td>
            </tr>
            <tr>
             <td class="heading2">Vendor Number:</td>
             <td>
              <asp:textbox id="txtEditVendorNumber" runat="server"></asp:textbox></td>
            </tr>
            <tr>
             <td class="heading2">Date Issued:</td>
             <td>
              <asp:textbox id="txtEditDateIssued" runat="server"></asp:textbox></td>
            </tr>
            <tr>
             <td colSpan="2">
              <asp:button id="cmdEdit" runat="server" Text="Save"></asp:button></td>
            </tr>
           </table>
          </td>
         </tr>
        </table>
       </asp:panel>

       <!-- Delete Panel -->
       <asp:panel id="pnlDelete" runat="server" visible="false">
        <table id="table6" cellpadding="10" width="600" align="center">
         <tr>
          <td class="info" valign="top">
           <p>To delete a person, please select them from the listbox.
           </p>
           <asp:listbox id="lstDelete" runat="server" Rows="12"></asp:listbox>
           <p>
            <!--<asp:Button id="cmdDelete" runat="server" Text="Delete"></asp:Button>-->
            <input type="button" id="cmdDelete2" value="Delete" OnClick="deleteperson()" /></p>
          </td>
         </tr>
        </table>
       </asp:panel><br />

       <div style="text-align: right"><a href="index.aspx">Index</a></div>
      </td>
      <td width="20" background="images/right_border.gif"></td>
     </tr>
     <tr>
      <td width="20" background="images/bottomleft_corner.gif" height="20"></td>
      <td background="images/bottom_border.gif" height="20"></td>
      <td width="20" background="images/bottomright_corner.gif" height="20"></td>
     </tr>
    </tbody>
   </table>
  </form>
 </body>
</html>

I haven't included the behind code because it wouldn't fit. The behind code just holds functions for executing the .NET Web Forms that are in the HTML page, if possible, it would be good if after the user clicks the Yes button from the confirmation alert the function, "cmdDelete" from the behind code is executed. I'm trying to create the popup with the user clicks the Delete button in the 'Delete' Panel shown in the HTML page. If anyone knows how I can go about doing this it would be really helpful and much appreciated, thank you.

Regards,
Alex Clifford
 
Hi Alex,

Rather than include the script in the HTML (you can do it that way but for what you are trying to do there is an easier way) you can just add an attribute for the button on the Page Load e.g.

Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Button1.Attributes.Add("onclick", "return confirm('Are you sure you wish to delete this record?');")
    End Sub

If the user click's cancel, the postback will not occur. If they click Yes then the postback will occur and you can just place all you code for that button as normal.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Thanks very much Ca8msm, works like a charm, much appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top