Solved

(web app) RequiredFieldValidator POSTS back and  doesnt work

Posted on 2008-06-17
16
174 Views
Last Modified: 2010-04-15
I've inherited a project and on a page, there's address fields and one textbox for zipcode. Next to this zip code textbox, there's a button to look up the zipcode and find the address.

I want to validate this field. If nothing is entered, no need to do a postback and go to the database.

2 issues: not only the code posts back, i dont see my error msg when I dont enter anything.
What's the point of RequiredFieldValidator if it's going to post back??

what am i missing?
<tr>
            <td>City</td>
            <td><asp:TextBox ID="txtCity" runat="server" ValidationGroup="vgAddress" /><asp:RequiredFieldValidator ID="rfvCity" ValidationGroup="vgAddress" ControlToValidate="txtCity" runat="server" SkinID="Error" ErrorMessage="City is required." Display="Dynamic" EnableClientScript="false" /></td>
        </tr>
 
....
 
   <tr>
            <td>Postal Code</td>
            <td><asp:TextBox ID="txtPostalCode" runat="server" Width="110px" ValidationGroup="vgAddress" />
            <asp:RequiredFieldValidator ValidationGroup="vgAddress" ID="rfvPostalCode" ControlToValidate="txtPostalCode" runat="server" SkinID="Error" ErrorMessage="Postal Code is required." Display="Dynamic" EnableClientScript="false" />&nbsp;
            <asp:Button ID="btnLookupByPostalCode" runat="server" Text="Lookup" UseSubmitBehavior="false" OnClick="btnLookupByPostalCode_Click" CausesValidation="true" /></td>
        </tr>
        </asp:Panel>

Open in new window

0
Comment
Question by:Camillia
  • 10
  • 6
16 Comments
 
LVL 22

Expert Comment

by:prairiedog
ID: 21803679
I think this is the problem: EnableClientScript="false"
Change it to "true" to see if it will work.
0
 
LVL 7

Author Comment

by:Camillia
ID: 21803796
let me try
0
 
LVL 7

Author Comment

by:Camillia
ID: 21803930
no, didnt work. I changed that to "true". changed validationgroup as well to keep this separate from other validated textboxes but no...makes a postback and doesnt validate.

I dont mind using Javascript to validate but i dont know how to combine OnClick="btnLookupByPostalCode_Click"

with my JS functon on the page. How do I call Validate JS function AND that click event.
<script ...>
 function validate()
 {
   .....
 }

 <tr>
            <td>Postal Code</td>
            <td><asp:TextBox ID="txtPostalCode" runat="server" Width="110px" ValidationGroup="vgAddress1" />
            <asp:RequiredFieldValidator ValidationGroup="vgAddress1" ID="rfvPostalCode" ControlToValidate="txtPostalCode" runat="server" SkinID="Error" ErrorMessage="Postal Code is required." Display="Dynamic" EnableClientScript="true" />&nbsp;
            <asp:Button ID="btnLookupByPostalCode" runat="server" ValidationGroup="vgAddress1" Text="Lookup" UseSubmitBehavior="false"    OnClick="btnLookupByPostalCode_Click" CausesValidation="true" /></td>
        </tr>

Open in new window

0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 22

Expert Comment

by:prairiedog
ID: 21803969
Sorry, it seems that you also need to remove this from your Button control: OnClick="btnLookupByPostalCode_Click"
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 21804072
In my test, I have to remove ValidationGroup as well to make it work as the way it should.
0
 
LVL 7

Author Comment

by:Camillia
ID: 21804093
No, tried that as well. Besides, I cant remove that onClick because if the user enters somethign in that textbox, then I want the onClick event to be executed..

so , if the textbox is blank, i want that validation to fire when user clicks "Lookup" button. It shouldnt execute the onClick event code.
      if the textbox is NOT blank, i want the onClick code to fire.

how can I do it with Javascript?...not sure how to call the onClick AND the JS code at the same time...
0
 
LVL 7

Author Comment

by:Camillia
ID: 21804143
just saw your msg about validation group. let me try that .
0
 
LVL 22

Accepted Solution

by:
prairiedog earned 500 total points
ID: 21804158
Yes, you can remove onClick. After you remove that, double-click the button, then an event handler will be created for you to handle the buttion click event, then just copy & paste your code into the new event handler. I believe onClick will cause post back.
0
 
LVL 7

Author Comment

by:Camillia
ID: 21804203
let me try the onClick.

I'm new to this code. Thanks for sticking with this.
0
 
LVL 7

Author Comment

by:Camillia
ID: 21804784
Nothing works. I've spent 4 hours on this and i just want to sit here and cry. Good that my manager is out of town. With this rate, i'll never get anything done!

1. I removed the onClick. Double clicked on the button. It created an event handler. I enter something in the textbox, it should go to that code but doesnt even make a postback now.

2. I have to have the validationgroup. There are other textboxes with validationGroups. If I remove it, that button tries to validate other boxes. I even renamed the validation group .

3. I cant now even use Javascript because if I use onClientClick="<js function>"... and enter something in the textbox, the code doesnt post back to go that event handler...

This is what I have... not sure what else to do... Any ideas? Thanks Kamila.
<tr>
            <td>Postal Code</td>
            <td><asp:TextBox ID="txtPostalCode" runat="server" Width="110px" ValidationGroup="vgAddress1"  />
            <asp:RequiredFieldValidator  ID="rfvPostalCode" ControlToValidate="txtPostalCode" runat="server" SkinID="Error" ErrorMessage="Postal Code is required." Display="Dynamic"  />&nbsp;
            <asp:Button ID="btnLookupByPostalCode" runat="server" ValidationGroup="vgAddress1" Text="Lookup"    CausesValidation="true"  /></td>
        </tr>
---********* This is the other button that validates:
  <asp:Button ID="btnSaveChanges" CausesValidation="true" UseSubmitBehavior="false" Text="Validate" OnClick="btnSaveChanges_Click" runat="server" ValidationGroup="vgAddress" />
        

Open in new window

0
 
LVL 7

Author Comment

by:Camillia
ID: 21804876
oh, it just validated with that code. Seems like there's a panel and hides behind the button...

But still....since I removed the onClick and if I enter a value...it doesnt go back to the code. Nothing happens. I put debug steps but doesnt go back to the code...
I even added:  UseSubmitBehavior="true" for the button...
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 21804983
Remember though, if the validation fails, no post back will occur, thus the event handler will not fire. It is just the way how validation works.
0
 
LVL 7

Author Comment

by:Camillia
ID: 21805682
But I enter a valid value...

So, if I dont enter anything, it shouldnt do a post back and it should display my error msg. *** This works now*** Validator works.

*** But since I removed the onClick...and I enter a VALID VALUE...it doesnt do a post back. Doesnt go to the click event of the button. I put a debug step and doesnt go back to the code.

I will try removing the button and adding it back in. This is what I have now:

 <td>Postal Code</td>
            <td><asp:TextBox ID="txtPostalCode" runat="server" Width="110px"  ValidationGroup="vgAddress1"  />
            <asp:RequiredFieldValidator  ID="rfvPostalCode" ControlToValidate="txtPostalCode" runat="server" SkinID="Error" ErrorMessage="Postal Code is required." Display="Dynamic"  />&nbsp;
            <asp:Button ID="btnLookupByPostalCode" runat="server"  ValidationGroup="vgAddress1" Text="Lookup"    CausesValidation="true"  /></td>

Open in new window

0
 
LVL 7

Author Comment

by:Camillia
ID: 21805769
Yeah, you're right...onClick makes it postBack but then if I remove it and double click on the button to create the event handler and then enter a VALID value..it wont post back but it should. Nothing happens....

I put a debug step and with onClick..it posts back and gets the value. WIthout it, it doesnt.

Let me delete and recreate the button. This is in a user-control and in a panel but i dont think that should make a diff...
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 21805792
Since you have ValidationGroup="vgAddress1", if makes me wonder if you have other requiredfieldValidators in this group that causes the problem.
0
 
LVL 7

Author Comment

by:Camillia
ID: 21806306
no, that wasnt it either. I renamed the validationGroup to "test". Removed the button , added a new one.  Double Clicked on it, created the onClick event. It add "onClick="whatever_click" to the tag itself when I double click on the button.

<asp:button runet="server" onClick="whatever_click" ...>

And if i remove it, the button wont post back with valid data.

Spent all day on this :(  will take a break from it now. If you can think of something, please post back.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question