PDA

View Full Version : .NET RequiredFieldValidator stops working after first submit


gzieve
08-24-2005, 04:17 PM
I am using VS.NET with VB.NET. I have a page with a submit button and a reset button and several text boxes. A RequiredFieldValidator ensures that the 2 required text boxes are filled. The reset button just clears out all the fields. The RequiredFieldValidator works only when the page is first loaded, but if I submit the page or reset the page, it no longer works....the page loads like it's not even there. If I go to another page on the site and then come back to that page, it works again. What do I have to change?

afterburn
08-24-2005, 04:58 PM
I have never seen that occur. Can you post your code?

gzieve
08-25-2005, 09:15 AM
I'm not sure how much you want, b/c there's a lot of code in the code behind page.

On the HTML page, these are the textboxes...


<TR>
<TD style="WIDTH: 126px; HEIGHT: 26px">
Study Code:*
</TD>
<TD style="WIDTH: 206px; HEIGHT: 26px">
<asp:textbox id="txtstudycode"
Runat="server" Columns="24" MaxLength="24"></asp:textbox>
</TD>
</TR>
<tr>
<TD style="WIDTH: 126px; HEIGHT: 28px">
Study Name:*
</TD>
<TD style="WIDTH: 206px; HEIGHT: 28px">
<asp:textbox id="txtstudyname" Runat="server" Columns="24"
MaxLength="24"></asp:textbox>
</TD>
</tr>


These are the validators....

<asp:RequiredFieldValidator id="valStudyCode" style="Z-INDEX: 104; LEFT: 24px;
POSITION: absolute; TOP: 192px" runat="server" Font-Size="Smaller" ControlToValidate="txtstudycode"
ErrorMessage="Required Field: Study Code is
Required"></asp:RequiredFieldValidator>
<asp:RequiredFieldValidator id="valStudyName" style="Z-INDEX: 105; LEFT: 24px;
POSITION: absolute; TOP: 208px" runat="server" Font-Size="Smaller"
ControlToValidate="txtstudyname"
ErrorMessage="Required Field: Study Name is
Required"></asp:RequiredFieldValidator></DIV>



Here is what I think may be relevant in the code behind page.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not (Page.IsPostBack) Then
chech_admin()
BindTheTA()
BindThePhase()
BindTheCSL()
End If
changetabs()
End Sub

Private Sub lnksubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnksubmit.Click
insert_study()
End Sub

Private Sub lnkReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkReset.Click
clear_all()
End Sub


Sub insert_study()
(code here)
End Sub

Sub clear_all()
(code here clears all fields)
End Sub

afterburn
08-25-2005, 09:20 AM
ok i have a few questions here. Where are those placed at??? I do not see them.

You should also do

Page.IsValid checks just as you would do Page.IsPostBack check to insure no re-binding of the data.

IsValid validates subcontrols also.

gzieve
08-25-2005, 09:36 AM
Sorry...I entered the my reply before I was finished (was tabbing down and hit post by mistake!)...I just went back and edited it, so take another look as there's more code now.

I just added Page.IsValid here, but it had no effect.

Private Sub lnksubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnksubmit.Click
If Page.IsValid Then
insert_study()
End If
End Sub

afterburn
08-25-2005, 09:56 AM
the reset button button ??? that should just be a plain html control not a webcontrol . I suggest that you use that instead.

gzieve
08-25-2005, 10:16 AM
I'll do that, or I'll just use javascript to validate instead...thanks!

afterburn
08-25-2005, 11:20 AM
Required field validators work well and if you use them then it not only checks on the client side. but on the server side if not validated correctly on client. So I would try to correct what you have unless you want to write more code for the server...