Solved

(web app) RequiredFieldValidator POSTS back and  doesnt work

Posted on 2008-06-17
16
170 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 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