Solved

(web app) RequiredFieldValidator POSTS back and  doesnt work

Posted on 2008-06-17
16
167 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now