Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

(web app) RequiredFieldValidator POSTS back and  doesnt work

Posted on 2008-06-17
16
Medium Priority
?
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

610 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