Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_beginners thread: Validation controls and AutoPostBack=True


Message #1 by "John Tyson" <jtyson@t...> on Mon, 13 May 2002 07:03:52 -0700
Hi,

I am building an .aspx page that has a number of controls that I want to
validate the user's input.  However, I also have a dropdownlist that is
populated by a stored procedure that has autopostback set to true, so
that when the user selects and option from the list a second
dropdownlist is populated based on their selection.

I have the submit button tied to a cmdValidation_Click sub, but the
validation controls try to validate the other controls whenever they are
changed too.  I only want the validation controls to run when the submit
button is clicked...I suspect this has to do with the autopostback being
set to true.  I tried setting clientsidescripting to false for the
validation controls but that didn't work.

Any suggestions???

Thanks,

John
Message #2 by "Dogers" <dogers@t...> on Mon, 13 May 2002 15:28:24 +0100
> I have the submit button tied to a cmdValidation_Click sub, 
> but the validation controls try to validate the other 
> controls whenever they are changed too.  I only want the 
> validation controls to run when the submit button is 
> clicked...I suspect this has to do with the autopostback 
> being set to true.  I tried setting clientsidescripting to 
> false for the validation controls but that didn't work.

From the sounds of things, youre having the user select an item then
submit the form, for the chosen result to be posted back? If so, this
isnt the right way to do it (or rather, its not the way I achieved it in
a recent project! :) you need to add a tag in the dropdownlist called
"onselectedindexchanged". This specifies a function for the server to
run when the data is posted back. Something along the lines of:

<asp:dropdownlist runat="server" id="list1" autopostback="true"
onselectedindexchanged="List1Change"/>

The further down-
Sub List1Change(sender As Object, e As System.EventArgs)
-- code to populate second list box--
End sub

When the user changes the listbox now, the page will reload with the
second list populated, with the first list containing the selected item.
The validation controls arent fired, as the form hasn't been submitted..

Hope that helps :)

-- 
Dogers (dogers@t...)
http://www.the-marines.com

Message #3 by "John Tyson" <jtyson@t...> on Mon, 13 May 2002 07:31:45 -0700
Hi Dogers,

Thanks for the response.

I actually have an onselectedindexchanged sub - sorry if I didn't
provide enough info in my first post (wanted to make sure it got read!)
The user selects a value from the first dropdownlist, which triggers
this sub, which populates the second dropdownlist from a stored
procedure, based on the value selected in the first box.  However, the
validator controls for those controls also light up - I don't know if
this is because of the autopostback, or if it is because I am populating
these dropdownlists from stored procedures using datareaders?

You can view what is happening here:
http://192.168.1.17/nwrehab/PAS_PatientRegVal.aspx

If it would help I am willing to post my code.

Thanks,

John


-----Original Message-----
From: Dogers [mailto:dogers@t...]
Sent: Monday, May 13, 2002 7:28 AM
To: aspx_beginners
Subject: [aspx_beginners] RE: Validation controls and 
AutoPostBack=3DTrue

> I have the submit button tied to a cmdValidation_Click sub,
> but the validation controls try to validate the other
> controls whenever they are changed too.  I only want the
> validation controls to run when the submit button is
> clicked...I suspect this has to do with the autopostback
> being set to true.  I tried setting clientsidescripting to
> false for the validation controls but that didn't work.

From the sounds of things, youre having the user select an item then
submit the form, for the chosen result to be posted back? If so, this
isnt the right way to do it (or rather, its not the way I achieved it in
a recent project! :) you need to add a tag in the dropdownlist called
"onselectedindexchanged". This specifies a function for the server to
run when the data is posted back. Something along the lines of:

<asp:dropdownlist runat=3D"server" id=3D"list1" autopostback=3D"true"
onselectedindexchanged=3D"List1Change"/>

The further down-
Sub List1Change(sender As Object, e As System.EventArgs)
-- code to populate second list box--
End sub

When the user changes the listbox now, the page will reload with the
second list populated, with the first list containing the selected item.
The validation controls arent fired, as the form hasn't been submitted..

Hope that helps :)

--
Dogers (dogers@t...)
http://www.the-marines.com


Message #4 by "Dogers" <dogers@t...> on Mon, 13 May 2002 16:17:33 +0100
> value selected in the first box.  However, the validator 
> controls for those controls also light up - I don't know if 
> this is because of the autopostback, or if it is because I am 
> populating these dropdownlists from stored procedures using 
> datareaders?

I used a data reader on mine and had no problems.. What are you actually
validating?

> You can view what is happening here: 
> http://192.168.1.17/nwrehab/PAS_PatientRegVal.aspx

I cant seem to access that.. I suspect its an internal IP for your/your
companies network

-- 
Dogers (dogers@t...)
http://www.the-marines.com

Message #5 by "John Tyson" <jtyson@t...> on Mon, 13 May 2002 08:18:05 -0700
Here is my .aspx page code (sorry if it is a bit cumbersome - I'm trying
to get it to work first before putting it in page behind; at least it
should give you an idea of what I'm trying to do/doing wrong?  Thanks):

<%@ Page Language=3D"vb"%>
<%@ Import Namespace=3D"System.Data" %>
<%@ Import Namespace=3D"System.Data.SqlClient" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
   <head>
      <title>Prior Advice System</title>
   </head>
   <script language=3D"VB" runat=3D"server">

      Sub cmdValidation_Click(s As Object, e As EventArgs)
      If Page.IsValid Then
         Response.Redirect("www.google.com")
      End If
      End Sub

      Sub Page_Load(s As Object, e As EventArgs)
      If Not Page.IsPostBack Then   
         Dim MyConnection As SqlConnection =3D New SqlConnection _
           ("server=3Dserver;uid=3Duid;pwd=3Dpwd;database=3Ddatabase")

         Dim MyCommand As SqlCommand =3D New SqlCommand()
         With MyCommand
            .Connection =3D MyConnection
            .CommandText =3D "sproc_GetProds"
            .CommandType =3D CommandType.StoredProcedure
         End With

         Dim MyDataReader As SqlDataReader

         Try
            MyConnection.Open()
            MyDataReader =3D MyCommand.ExecuteReader()
            lstPayerProd.DataSource =3D MyDataReader
            lstPayerProd.DataBind()
         Catch myException As Exception
            Response.Write("An error has occurred: " &
myException.ToString())

         Finally
           If Not MyDataReader is Nothing Then
             MyDataReader.Close()
           End If

           lstPayerProd.Items.Insert(0, "Select a Product")
             lstPayerProd.SelectedIndex =3D 0
             lstPayerProd.SelectedItem.Value =3D 0
             lstPayerProd.SelectedItem.Text =3D "Select a Product"
         End Try

         With MyCommand
            .CommandText =3D "sproc_GetBodyIndex"
         End With

         Try
            MyDataReader =3D MyCommand.ExecuteReader()
            lstBodyIndex.DataSource =3D MyDataReader
            lstBodyIndex.DataBind()
         Catch myException As Exception
            Response.Write("An error has occurred: " &
myException.ToString())

         Finally
           If Not MyDataReader is Nothing Then
             MyDataReader.Close()
           End If

           lstBodyIndex.Items.Insert(0, "Select an Anatomical Group")
             lstBodyIndex.SelectedIndex =3D 0
             lstBodyIndex.SelectedItem.Value =3D 0
             lstBodyIndex.SelectedItem.Text =3D "Select an Anatomical
Group"
         End Try

         MyConnection.Close
      End If
      End Sub

      Sub payerprod_change(s As Object, e As EventArgs)
         Dim MyConnection As SqlConnection =3D New SqlConnection _
            ("server=3Dserver;uid=3Duid;pwd=3Dpwd;database=3Ddatabase")
         Dim MyCommand As SqlCommand =3D New SqlCommand
("sproc_GetProviders", MyConnection)
         MyCommand.CommandType =3D CommandType.StoredProcedure

         Dim prodParam As SqlParameter =3D MyCommand.Parameters.Add _
            ("@prodParam", SqlDbType.Int)
         prodParam.Direction =3D ParameterDirection.Input
         prodParam.Value =3D CInt(lstPayerProd.SelectedItem.Value)

         Dim MyDataReader As SqlDataReader

         Try
            MyConnection.Open()
            MyDataReader =3D
MyCommand.ExecuteReader(CommandBehavior.CloseConnection)
            lstProviders.DataSource =3D MyDataReader
            lstProviders.DataBind()
         Catch myException As Exception
            Response.Write("An error has occurred: " &
myException.ToString())

         Finally
           If Not MyDataReader is Nothing Then
             MyDataReader.Close()
           End If

           lstProviders.Items.Insert(0, "Select a Provider")
             lstProviders.SelectedIndex =3D 0
             lstProviders.SelectedItem.Value =3D 0
             lstProviders.SelectedItem.Text =3D "Select a Provider"
         End Try

      End Sub
   </script>

   <body>
      <form runat=3D"server">
         <p>
            Payer Product:
            <br/>
            <asp:DropDownList id=3D"lstPayerProd"
DataValueField=3D"productID" DataTextField=3D"product"
onSelectedIndexChanged=3D"payerprod_change" runat=3D"server"
AutoPostBack=3D"True" />
            <asp:CompareValidator id=3D"valPayerProd" runat=3D"server"
               ControlToValidate=3D"lstPayerProd"
               ValueToCompare=3D"Select a Product"
               Type=3D"String"
               Operator=3D"Equal"
               ErrorMessage=3D"Please select a Product."
               InitialValue=3D"">*
            </asp:CompareValidator>
            <br/>Provider ID:
            <br/>
            <asp:DropDownList id=3D"lstProviders"
DataValueField=3D"therapistID" DataTextField=3D"therapist" 
runat=3D"server" />
            <asp:RequiredFieldValidator id=3D"vallstProviders"
runat=3D"server"
               controlToValidate=3D"lstProviders"
               errorMessage=3D"Provider cannot be blank."
               initialValue=3D""
               display=3D"static">*
            </asp:RequiredFieldValidator>
            <asp:CompareValidator id=3D"vallstProviders2" 
runat=3D"server"
               ControlToValidate=3D"lstProviders"
               ValueToCompare=3D"Please Select a Provider."
               Type=3D"String"
               Operator=3D"Equal"
               ErrorMessage=3D"Please select a Provider."
               display=3D"static"
               InitialValue=3D"">*
            </asp:CompareValidator>
            <br/>Patient Identification:
            <br/>Payer Patient ID:
            <br/>
            <asp:TextBox id=3D"txtPayerPatID" runat=3D"server" />
            <asp:RequiredFieldValidator id=3D"valPayerPatID"
runat=3D"server"
               controlToValidate=3D"txtPayerPatID"
               errorMessage=3D"Payer Patient ID cannot be blank."
               initialValue=3D""
               display=3D"static">*
            </asp:RequiredFieldValidator>
            <br/>Clinic Patient ID:
            <br/>
            <asp:TextBox id=3D"txtClinPatID" runat=3D"server" />
            <asp:RequiredFieldValidator id=3D"valClinPatID" 
runat=3D"server"
               controlToValidate=3D"txtClinPatID"
               errorMessage=3D"Clinic Patient ID cannot be blank."
               initialValue=3D""
               display=3D"static">*
            </asp:RequiredFieldValidator>
            <br/>Anatomical Group:
            <br/>
            <asp:DropDownList id=3D"lstBodyIndex" runat=3D"server"
DataValueField=3D"bodyindexID" DataTextField=3D"bodyindex"
AutoPostBack=3D"True" />
            <asp:CompareValidator id=3D"valBodyIndex" runat=3D"server"
               ControlToValidate=3D"lstBodyIndex"
               ValueToCompare=3D"Select an Anatomical Group"
               Type=3D"String"
               Operator=3D"Equal"
               ErrorMessage=3D"Please select an Anatomical Group."
               InitialValue=3D""
               display=3D"static">*
            </asp:CompareValidator>
            <br/>Primary ICD-9:
            <br/>
            <asp:TextBox id=3D"txtPriICD9" runat=3D"server" />
            <asp:RequiredFieldValidator id=3D"valPriICD9" 
runat=3D"server"
               controlToValidate=3D"txtPriICD9"
               errorMessage=3D"Primary ICD-9 cannot be blank."
               initialValue=3D""
               display=3D"static">*
            </asp:RequiredFieldValidator>
            <br/>Secondary ICD-9:
            <br/>
            <asp:TextBox id=3D"TxtSecICD9" runat=3D"server" />
            <br/>
            <br/>Initial Date of Service:
            <br/>
            <asp:TextBox id=3D"txtInitDOS" runat=3D"server" />
            <asp:RequiredFieldValidator id=3D"valInitDOS" 
runat=3D"server"
               controlToValidate=3D"txtInitDOS"
               errorMessage=3D"Initial Date of Service cannot be blank."
               initialValue=3D""
               display=3D"static">*
            </asp:RequiredFieldValidator>
            <asp:RegularExpressionValidator id=3D"valInitDOS2"
runat=3D"server"
               controlToValidate=3D"txtInitDOS"
               ErrorMessage=3D"Please enter the date as mm/dd/yyyy."
               InitialValue=3D""
               ValidationExpression=3D"\d{1,2}\/\d{1,2}\/\d{2,4}$"
               display=3D"static">*
            </asp:RegularExpressionValidator>
            <br/>
            <asp:Button id=3D"btnSubmit" Text=3D"Submit"
onClick=3D"cmdValidation_Click" runat=3D"server" />
            <br/>
            <asp:ValidationSummary id=3D"valSummary" runat=3D"server" />
         </p>
      </form>
   </body>
</html>

-----Original Message-----
From: Dogers [mailto:dogers@t...]
Sent: Monday, May 13, 2002 8:18 AM
To: aspx_beginners
Subject: [aspx_beginners] RE: Validation controls and 
AutoPostBack=3DTrue

> value selected in the first box.  However, the validator
> controls for those controls also light up - I don't know if
> this is because of the autopostback, or if it is because I am
> populating these dropdownlists from stored procedures using
> datareaders?

I used a data reader on mine and had no problems.. What are you actually
validating?

> You can view what is happening here:
> http://192.168.1.17/nwrehab/PAS_PatientRegVal.aspx

I cant seem to access that.. I suspect its an internal IP for your/your
companies network

--
Dogers (dogers@t...)
http://www.the-marines.com


Message #6 by "Dogers" <dogers@t...> on Mon, 13 May 2002 18:55:02 +0100
>             <asp:TextBox id="txtPayerPatID" runat="server" />
>             <asp:RequiredFieldValidator id="valPayerPatID" 
> runat="server"
>                controlToValidate="txtPayerPatID"
>                errorMessage="Payer Patient ID cannot be blank."
>                initialValue=""
>                display="static">*
>             </asp:RequiredFieldValidator>

I have a feeling its because of the required field validators.. If the
validation controls show up when the page comes back, id imagine its
because the servers processed them seeing that theres nothing in the
controls.. not really sure how to get round that, my validation controls
were on the combo boxes, and did comparisons on the value.. You could
try running the validation on the client side, as I don't think it would
show up then until the client tabbed in and out of the control

-- 
Dogers (dogers@t...)
http://www.the-marines.com


  Return to Index