Add Confirmation After Controls Validation on Client
page 1 of 1
Published: 03 Mar 2004
Unedited - Community Contributed
Abstract
Add a confirmation dialog to a web form after performing server control validation.
by Tim Curtin
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 8946/ 15

Confirm after Validation

This is a test page that we add a simple HTML button with an ID of btnSave with some script(Not web form control). No need or 'Run as Server Control'. I added an HTML Hidden control and initialize it in the checkUIEntry() function.
First we force the Page_ClientValidate() function in the WebUIValidation.js file that the framework downloads. If that returns true, then prompt the web visitor to submit the page.

Then, in the Page_Load event, we check for IsPostBack and the value of the Hidden control.

 

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="testWeb.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <title>WebForm1</title>
  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
  <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
  <meta content="JavaScript" name="vs_defaultClientScript">
  <meta content="
http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <script language="javascript">
   function checkUIEntry(){
    if(Page_ClientValidate()){
     if(confirm("Save?")){
      Form1.submitControl.value="saveButton"
      document.Form1.submit()
     }
    }
   }
  </script>
  <form id="Form1" method="post" runat="server">
   <asp:textbox id="TextBox1" style="Z-INDEX: 101; LEFT: 128px; POSITION: absolute; TOP: 24px" runat="server"></asp:textbox>
   <asp:button id="Button1" style="Z-INDEX: 102; LEFT: 56px; POSITION: absolute; TOP: 80px" runat="server"Text="Button"></asp:button>
   <asp:label id="Label1" style="Z-INDEX: 103; LEFT: 40px; POSITION: absolute; TOP: 24px" runat="server">Enter Value:</asp:label>
   <asp:requiredfieldvalidator id="RequiredFieldValidator1" style="Z-INDEX: 104; LEFT: 296px; POSITION: absolute; TOP: 24px" runat="server" ControlToValidate="TextBox1" ErrorMessage="Required"></asp:requiredfieldvalidator>
   <asp:label id="Label2" style="Z-INDEX: 105; LEFT: 64px; POSITION: absolute; TOP: 48px" runat="server" ForeColor="#0000C0">Label</asp:label>
   <INPUT id="btnSave" style="Z-INDEX: 106; LEFT: 240px; POSITION: absolute; TOP: 80px" onclick="checkUIEntry()" type="button" value="Save">
   <INPUT id="submitControl" style="Z-INDEX: 107; LEFT: 296px; POSITION: absolute; TOP: 56px" type="hidden" name="Hidden1" runat="server">
  </form>
 </body>
</HTML>


'******************** CODE BEHIND  ******************************************************************
Public Class WebForm1
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents RequiredFieldValidator1 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents Label2 As System.Web.UI.WebControls.Label
    Protected WithEvents submitControl As System.Web.UI.HtmlControls.HtmlInputHidden

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

        If IsPostBack Then
            viewstate("counter") += 1
            Me.Label2.Text = "Posted " & viewstate("counter")
        Else
            viewstate("counter") = 0
        End If

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Page.IsValid Then
            Me.Label2.Text = "IsValid"
        Else
            Me.Label2.Text = "Is Invalid"
        End If
    End Sub
End Class



User Comments

Title: 1   
Name: 212
Date: 2012-11-29 7:24:06 AM
Comment:
12121
Title: sdfdsf   
Name: sdfsd
Date: 2012-07-31 12:01:42 PM
Comment:
dfsd
Title: vaidationGroup is undefined   
Name: Ron
Date: 2008-05-22 11:15:06 PM
Comment:
people here say it works, but i'm getting the above message. that is because you're not setting a parameter to the Page_ClientValidate. don't you get this JS message?
is there any special setting for this parameter? what should it be?
Title: Re: you idoit   
Name: Alexis
Date: 2007-12-18 11:34:22 AM
Comment:
1) translation between C# and VB is fairly easy for most with half an ounce of their own intelligence
2) calling someone idiot will not encourage them to help you in the least bit
3) increase your image of semi-intelligence by learning how to either spell or use spell check

Thank you for your article, Tim. It's helped me out a lot. :)
Title: thanks   
Name: Roy
Date: 2006-06-21 11:47:35 PM
Comment:
Thanks for this little code i get my validation to finally work. Page_ClientValidate() is what i needed
Title: thanks for the good article   
Name: Larisa
Date: 2006-02-02 12:33:18 PM
Comment:
I am strugling with confirmation in Visual Studio after validation. The article is good, though I still need capture the login name and save it and confirm that the form is submitted

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-25 8:39:44 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search