Am spending hours trying to get to the bottom of this. I have a user control within a hidden panel which is made visible after a postback (my first button checks a validation for a patient number). The panel has a from and a second submit button that will
send data back - at this point I do further validation...
I have a RequiredFieldValidator on a text box but when my button is clicked the Page.IsValid always returns True, even if the field is not filled in.
here is the form snippit:
<asp:TextBox ID="TextBox3" runat="server" ValidationGroup="ValGroup1"></asp:TextBox>
<asp:RequiredFieldValidator ID="ReqV1" ValidationGroup="ValGroup1" runat="server" EnableClientScript="false" ControlToValidate="TextBox3" ErrorMessage="This is a required field"></asp:RequiredFieldValidator>
</td>
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Page.Validate()
If Page.IsValid Then' DO THE WORK HERE
Try
SubmitMessage.Text = Page.IsValid.ToString
Catch ex As Exception
SubmitMessage.ForeColor = Drawing.Color.DarkRed
SubmitMessage.Text = "Error, please review form and try again. " & ex.Message
End Try
Else
Panel1.Visible = True
SubmitMessage.Text = "Validation error, please check form messages and try again."End If
End Sub
Any help with this very much appreciated, thanks in advance.
Hi, no it is still returning True even if the field is left blank - I tried this earlier with and without the validation group. I am wondering whether it is because I called a validation during my first button click and the page is posted back? I cant figure
it out.
It is not working on my page - perhapse something to do with it a) being a user control or b) it is the second time a validation has been called. It is frustrating that the code above by itself works fine but not on my page. Here is the complete code...
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Me.IsPostBack = False Then
Button2.Visible = False
Panel1.Visible = False
Button3.Attributes.Add("onclick", "return confirm('You are about to cancel. Patient details will not be saved" & _
" and you will return to the patient search. Are you sure you want to cancel?');")
Button2.Attributes.Add("onclick", "return confirm('Are you sure you wish to submit the Serious Events information?');")
Else
Button2.Visible = True
If GridView1.EmptyDataText = "<b>Unknown Trial Number.</b> Please enter a valid trial number and click the Retrieve button." Then ' v1.02
Panel1.Visible = False
Else
Panel1.Visible = True
End If
Button3.Attributes.Add("onclick", "return confirm('You are about to cancel. Patient details will not be saved" & _
" and you will return to the patient search. Are you sure you want to cancel?');")
Button2.Attributes.Add("onclick", "return confirm('Are you sure you wish to submit the Serious Events information?');")
End If
End Sub
Protected Sub GridView1_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.Init
If Me.IsPostBack = False Then
GridView1.EmptyDataText = "Please enter the patient trial number in the box below " & _
"and press the <b>Retrieve</b> button."Else
GridView1.EmptyDataText = "<b>Unknown Trial Number.</b> Please enter a valid trial number and click the Retrieve button."End If
End Sub
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim PatientID As Integer = DataBinder.Eval(e.Row.DataItem, "PatientNo")
Session("PatientID") = PatientID
Button2.Visible = True
Button1.Visible = False
Panel1.Visible = True
SubmitMessage.Text = ""End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If Validator1.IsValid = False Then
Button2.Visible = False
Panel1.Visible = False
End If
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Page.Validate()
Page.Validate("ValGroup1")
If Page.IsValid Then' DO THE WORK HERE
Try
SubmitMessage.Text = Page.IsValid.ToString
Catch ex As Exception
SubmitMessage.ForeColor = Drawing.Color.DarkRed
SubmitMessage.Text = "Error, please review form and try again. " & ex.Message
End Try
Else
Panel1.Visible = True
SubmitMessage.Text = "Validation error, please check form messages and try again."End If
End Sub
Still, validation is true even if field is left blank, valid should be false and the user should see the validation error, but it is not.
Not so long ago I met the same problem. Thats's may solution (sorry, it's in c# but it is very readable)
I used a customvalidator-control to check the validity of my input. On the moment of submission I check for the Page.IsValid. Believe me it works fine ...
good luck
frans
protected void Page_Load(object sender, EventArgs e)
{
if( !IsPostBack )
{
// Initializition of the controls
// ....
// Set panels
Panel1.Visible = true;
Panel2.Visible = false;
Panel3.Visible = false;
}
}
protected void btnNext_Click(object sender, EventArgs e)
{
//
// If page is valid we load the data to panel 2
// otherwise an error will be thrown
//
if( Page.IsValid )
{
//
// refresh date
//
RefreshPanel2();
Panel1.Visible = false;
Panel2.Visible = true;
}
}
private void RefreshPanel2()
{
lblGegevens.Text =
"Dit zijn de gegevens: <br />" +
"<br />" +
"Naam: " + tbxNaam.Text + "<br />" +
"Klas: " + ddlKlas.SelectedItem + "<br />" +
"Email: " + tbxEmail.Text + "<br />";
}
protected void btnBack_Click(object sender, EventArgs e)
{
Panel2.Visible = false;
Panel1.Visible = true;
}
protected void btnToEnd_Click(object sender, EventArgs e)
{
Panel2.Visible = false;
Panel3.Visible = true;
}
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
//
// We check the validity of the content of a textbox
// (in my example the emailaddress must contain the content
// of the textbox tbxNummer)
//
if (!tbxEmail.Text.Contains(tbxNummer.Text))
args.IsValid = false;
}
Still no joy even if using a custom validator, it still returns True each time. Here is the modified code. I made sure whatever is done it should return false - but each time the button is clicked isValid always returns True -
Sub CustomValidate(ByVal sender As Object, ByVal args As ServerValidateEventArgs)
UPDATE: As I mentioned earlier, the controls I need to validate are in a panel which is initially hidden on page load. After one postback the panel is visible - this is where I validate my other controls and get a problem.
Now, if I go through my code and comment out all my panel1.visible = false statements, my validation works fine. Obviously I need to hide my panel at first - so why does this cause my validation to fail? It looks like I am getting close to a resolution
now, I just need to make sure the hiding and unhiding of my panel does not effect my validation outcome - any ideas?
First a disclaimer... I haven't read through the posts on this thread.
In short, if the panel isn't visible, then the controls have not been rendered, and thus there is nothing to validate. If I am missing something, my apologies.
If you have a page where you want invisible items validated (i.e. if doing a mutli-step work flow or whatever), instead of making the panel invisible, just use css to make it hidden. This way you won't see them, but they'll be out there and the validation
will work fine.
in css, add: .invisible { visibility:hidden; display:none;}
Then just set panel.CssClass = "invisible"
~Rick
Please mark the post as ANSWER if it helps you
Disclaimer: Just my opinion. Not my employer or anyone else....
crossbyname
Member
8 Points
24 Posts
Page.IsValid always True
Jun 08, 2009 02:01 PM|LINK
Hi,
Am spending hours trying to get to the bottom of this. I have a user control within a hidden panel which is made visible after a postback (my first button checks a validation for a patient number). The panel has a from and a second submit button that will send data back - at this point I do further validation...
I have a RequiredFieldValidator on a text box but when my button is clicked the Page.IsValid always returns True, even if the field is not filled in.
here is the form snippit:
<asp:TextBox ID="TextBox3" runat="server" ValidationGroup="ValGroup1"></asp:TextBox> <asp:RequiredFieldValidator ID="ReqV1" ValidationGroup="ValGroup1" runat="server" EnableClientScript="false" ControlToValidate="TextBox3" ErrorMessage="This is a required field"></asp:RequiredFieldValidator> </td>here is the code behing snippit:
Any help with this very much appreciated, thanks in advance.
Tim
MetalAsp.Net
All-Star
112718 Points
18367 Posts
Moderator
Re: Page.IsValid always True
Jun 08, 2009 02:29 PM|LINK
Try using Page.Validate("ValGroup1") and see if it works.
crossbyname
Member
8 Points
24 Posts
Re: Page.IsValid always True
Jun 08, 2009 03:15 PM|LINK
Hi, no it is still returning True even if the field is left blank - I tried this earlier with and without the validation group. I am wondering whether it is because I called a validation during my first button click and the page is posted back? I cant figure it out.
Ram Reddy Me...
Star
9604 Points
1314 Posts
Re: Page.IsValid always True
Jun 08, 2009 03:34 PM|LINK
This wotks fine for me.
<
asp:TextBox ID="TextBox3" runat="server" ValidationGroup="ValGroup1"></asp:TextBox> <asp:RequiredFieldValidator ID="ReqV1" ValidationGroup="ValGroup1" runat="server" EnableClientScript="false" ControlToValidate="TextBox3" ErrorMessage="This is a required field"></asp:RequiredFieldValidator> <asp:Button ID="Button2" runat="server" Text="Continue" ValidationGroup="ValGroup1" OnClick="Button2_Click" CausesValidation="true" />Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) If Page.IsValid Then End If End SubAbhiram Reddy Mekha
crossbyname
Member
8 Points
24 Posts
Re: Page.IsValid always True
Jun 08, 2009 03:45 PM|LINK
It is not working on my page - perhapse something to do with it a) being a user control or b) it is the second time a validation has been called. It is frustrating that the code above by itself works fine but not on my page. Here is the complete code...
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:AML17_TestConnectionString %>" SelectCommand="SELECT [RandomisationNo], [Firstname], [MidInitial], [Lastname], [DOB], [PatientNo], [amlFLT3ID], [AdultAPL], [AdultChild] FROM [VW_amlFLTrand] WHERE ([RandomisationNo] = @RandomisationNo)"> <SelectParameters> <asp:ControlParameter ControlID="TextBox1" Name="RandomisationNo" PropertyName="Text" Type="Int32" /> </SelectParameters> </asp:SqlDataSource> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" GridLines="None" BackColor="#FFCC99" HeaderStyle-BackColor="#151355" HeaderStyle-ForeColor="White" BorderStyle="None" Width="625px" HorizontalAlign="Center" DataSourceID="SqlDataSource1"> <Columns> <asp:BoundField DataField="RandomisationNo" HeaderText="Trial Number" /> <asp:BoundField DataField="Firstname" HeaderText="First name" /> <asp:BoundField DataField="MidInitial" HeaderText="Mid Initial" visible="false" /> <asp:BoundField DataField="Lastname" HeaderText="Last name" /> <asp:BoundField DataField="DOB" HeaderText="DOB" DataFormatString="{0:d}" /> <asp:BoundField DataField="PatientNo" HeaderText="Patient No" visible="false" /> <asp:BoundField DataField="amlFLT3ID" HeaderText="" visible="false" /> <asp:BoundField DataField="AdultAPL" HeaderText="" visible="true" /> </Columns> <HeaderStyle BackColor="#151355" ForeColor="White" /> </asp:GridView> <p> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" PostBackUrl="~/forms/AdverseEvents.aspx" Text="Retrieve" /> <asp:RangeValidator ID="Validator1" runat="server" ErrorMessage="* This number is not in the range (1001 to 9999)." ControlToValidate="Textbox1" MaximumValue="9999" MinimumValue="1001" Type="Integer" /> </p> <asp:Panel ID="Panel1" runat="server"> <p> Thank you. Patient details have successfully been retrieved. Please complete information below and click the Continue button to submit the form. </p> <br /> <b>Associated Drug (please tick all that apply)</b><table class="style1"> <tr> <td class="style4" colspan="4"> <asp:CheckBoxList ID="CBLDrugs" runat="server" RepeatColumns="5" RepeatDirection="Horizontal"> </asp:CheckBoxList> </td> </tr> <tr> <td class="style4"> Other (Specify):</td> <td class="style4" colspan="2"> <asp:TextBox ID="TextBox2" runat="server" Width="251px"></asp:TextBox> </td> <td class="style6" style="margin-left: 80px"> </td> </tr> <tr> <td class="style4"> Treatment Start Date:</td> <td class="style4"> <asp:TextBox ID="TextBox3" runat="server" ValidationGroup="ValGroup1"></asp:TextBox> <%-- <asp:RangeValidator Type="Date" ID="RangeV1" runat="server" EnableClientScript="false" MinimumValue="01/01/2009" MaximumValue="31/12/2020" ControlToValidate="TextBox3" ErrorMessage="Please enter the correct date format DD/MM/YYYY"> </asp:RangeValidator>--%> <asp:RequiredFieldValidator ID="ReqV1" ValidationGroup="ValGroup1" runat="server" EnableClientScript="false" ControlToValidate="TextBox3" ErrorMessage="This is a required field"></asp:RequiredFieldValidator> </td> <td class="style5"> Treatment Stop Date:</td> <td class="style6" style="margin-left: 80px"> <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox> </td> </tr> <tr> <td class="style4"> or continuing?:</td> <td class="style4"> <asp:CheckBox ID="CBDrugCont" runat="server" /> </td> <td class="style5"> </td> <td class="style6" style="margin-left: 80px"> </td> </tr> </table> <br /> <br /> <br /> <br /> <hr /> <div id="SubmitMes"> <p style="text-align: right"> <asp:Button ID="Button2" runat="server" Text="Continue" ValidationGroup="ValGroup1" CausesValidation="true" /> <asp:Button ID="Button3" runat="server" Text="Cancel" /> </p> </div> </asp:Panel> <asp:Label ID="SubmitMessage" runat="server" Text="" ></asp:Label> <asp:Label ID="Label1" runat="server"></asp:Label> <asp:Label ID="LblPatientID" runat="server" Visible="false"></asp:Label> <asp:Label ID="LblEmailBody" runat="server"></asp:Label> <p> </p>And more of the code behind...
Still, validation is true even if field is left blank, valid should be false and the user should see the validation error, but it is not.frvdm
Member
80 Points
82 Posts
Re: Page.IsValid always True
Jun 08, 2009 04:52 PM|LINK
Hi
Not so long ago I met the same problem. Thats's may solution (sorry, it's in c# but it is very readable)
I used a customvalidator-control to check the validity of my input. On the moment of submission I check for the Page.IsValid. Believe me it works fine ...
good luck
frans
protected void Page_Load(object sender, EventArgs e) { if( !IsPostBack ) { // Initializition of the controls // .... // Set panels Panel1.Visible = true; Panel2.Visible = false; Panel3.Visible = false; } } protected void btnNext_Click(object sender, EventArgs e) { // // If page is valid we load the data to panel 2 // otherwise an error will be thrown // if( Page.IsValid ) { // // refresh date // RefreshPanel2(); Panel1.Visible = false; Panel2.Visible = true; } } private void RefreshPanel2() { lblGegevens.Text = "Dit zijn de gegevens: <br />" + "<br />" + "Naam: " + tbxNaam.Text + "<br />" + "Klas: " + ddlKlas.SelectedItem + "<br />" + "Email: " + tbxEmail.Text + "<br />"; } protected void btnBack_Click(object sender, EventArgs e) { Panel2.Visible = false; Panel1.Visible = true; } protected void btnToEnd_Click(object sender, EventArgs e) { Panel2.Visible = false; Panel3.Visible = true; } protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) { // // We check the validity of the content of a textbox // (in my example the emailaddress must contain the content // of the textbox tbxNummer) // if (!tbxEmail.Text.Contains(tbxNummer.Text)) args.IsValid = false; }crossbyname
Member
8 Points
24 Posts
Re: Page.IsValid always True
Jun 08, 2009 07:21 PM|LINK
Still no joy even if using a custom validator, it still returns True each time. Here is the modified code. I made sure whatever is done it should return false - but each time the button is clicked isValid always returns True -
crossbyname
Member
8 Points
24 Posts
Re: Page.IsValid always True
Jun 08, 2009 08:37 PM|LINK
UPDATE: As I mentioned earlier, the controls I need to validate are in a panel which is initially hidden on page load. After one postback the panel is visible - this is where I validate my other controls and get a problem.
Now, if I go through my code and comment out all my panel1.visible = false statements, my validation works fine. Obviously I need to hide my panel at first - so why does this cause my validation to fail? It looks like I am getting close to a resolution now, I just need to make sure the hiding and unhiding of my panel does not effect my validation outcome - any ideas?
Xequence
Contributor
4516 Points
1575 Posts
Re: Page.IsValid always True
Jun 08, 2009 09:12 PM|LINK
try using findControl to locate your userControl items.
Credentials
Rick Matthys
Contributor
2794 Points
406 Posts
Re: Page.IsValid always True
Jun 08, 2009 11:23 PM|LINK
First a disclaimer... I haven't read through the posts on this thread.
In short, if the panel isn't visible, then the controls have not been rendered, and thus there is nothing to validate. If I am missing something, my apologies.
If you have a page where you want invisible items validated (i.e. if doing a mutli-step work flow or whatever), instead of making the panel invisible, just use css to make it hidden. This way you won't see them, but they'll be out there and the validation will work fine.
in css, add: .invisible { visibility:hidden; display:none; }
Then just set panel.CssClass = "invisible"
~RickDisclaimer: Just my opinion. Not my employer or anyone else....