• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

(web app) RequiredFieldValidator POSTS back and doesnt work

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
Camillia
Asked:
Camillia
  • 10
  • 6
1 Solution
 
prairiedogCommented:
I think this is the problem: EnableClientScript="false"
Change it to "true" to see if it will work.
0
 
CamilliaAuthor Commented:
let me try
0
 
CamilliaAuthor Commented:
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
prairiedogCommented:
Sorry, it seems that you also need to remove this from your Button control: OnClick="btnLookupByPostalCode_Click"
0
 
prairiedogCommented:
In my test, I have to remove ValidationGroup as well to make it work as the way it should.
0
 
CamilliaAuthor Commented:
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
 
CamilliaAuthor Commented:
just saw your msg about validation group. let me try that .
0
 
prairiedogCommented:
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
 
CamilliaAuthor Commented:
let me try the onClick.

I'm new to this code. Thanks for sticking with this.
0
 
CamilliaAuthor Commented:
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
 
CamilliaAuthor Commented:
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
 
prairiedogCommented:
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
 
CamilliaAuthor Commented:
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
 
CamilliaAuthor Commented:
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
 
prairiedogCommented:
Since you have ValidationGroup="vgAddress1", if makes me wonder if you have other requiredfieldValidators in this group that causes the problem.
0
 
CamilliaAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 10
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now