Page.IsValid changes to false after button click

A

Andrew Jocelyn

Hi

I have couple of dropdown controls each with a requiredfieldvalidator. I
clear the dropdownlist in the button click event. When the page is rendered
the dropdown validator already flags it's error message. I checked the
IsValid property in the prerender event and it says it's false.

protected void Button_Click(object sender, EventArgs e)
{
ddl.ClearSelection();
Trace.Write(Page.Validators[0].IsValid.ToString()); // returns True
}

protected override void OnPreRender(EventArgs e)
{
Trace.Write(Page.Validators[0].IsValid.ToString()); // returns False
base.OnPreRender(e);
}

What could be happening to change the IsValid property to false? I can't see
anything in my code which would do this. What's the best way to debug this?
What other checks should I be making?

Many thanks
Andrew
 
B

bruce barker

Page.IsValid runs the validation logic. so if you clear a selection, then use
Page.IsValid to run a validation check, it will fail.

-- bruce (sqlwork.com)
 
A

Andrew Jocelyn

Hi Bruce

That doesn't seem to be the case in my simple test with a single validator
on a page. The Page.IsValid value does not change how ever many times I call
it. However it does change in my project page which contains a FormView
control with a number of UserControls. What I don't seem to be able to do is
find out where or why it's changing.

Any ideas?

Thanks
Andrew
 
S

Steven Cheng [MSFT]

Hi Andrew,

According to your description, I've performed some tests, it seems a simple
page with dropdownlist and requiredFieldValidator will not produce the
exact behavior. Here is a simple test page I used:

========aspx============
<form id="form1" runat="server">
<div>

</div>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem></asp:ListItem>
<asp:ListItem>aaa</asp:ListItem>
<asp:ListItem>bbb</asp:ListItem>
<asp:ListItem>ccc</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="DropDownList1"
ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
<br />
<br />
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem></asp:ListItem>
<asp:ListItem>aaa</asp:ListItem>
<asp:ListItem>bbb</asp:ListItem>
<asp:ListItem>ccc</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="DropDownList2"
ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
<br />
<br />
<asp:DropDownList ID="DropDownList3" runat="server">
<asp:ListItem></asp:ListItem>
<asp:ListItem>aaa</asp:ListItem>
<asp:ListItem>bbb</asp:ListItem>
<asp:ListItem>ccc</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="DropDownList3"
ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
<br />
<br />
<asp:Button ID="btnPost" runat="server" onclick="btnPost_Click"
Text="Post Button" />
<asp:Button ID="btnClear" runat="server" onclick="btnClear_Click"
Text="Clear Button" />
</form>


=-========code behind=======
public partial class ValidatePage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnPost_Click(object sender, EventArgs e)
{

}
protected void btnClear_Click(object sender, EventArgs e)
{
Response.Write("<br/>Page.IsValid: " + Page.Validators[0].IsValid);

DropDownList1.ClearSelection();
DropDownList2.ClearSelection();
DropDownList3.ClearSelection();

Response.Write("<br/>Page.IsValid: " + Page.Validators[0].IsValid);
}

protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);

Response.Write("<br/>Page.IsValid: " + Page.Validators[0].IsValid);
}
}
=======================

It seems as long as the page postback(when I click the Clear Button) and
passed client-side validation, the server-side validation won't return
false after I "ClearSelection" on the dropdownlists.

As you mentioned that there are other forms and usercontrols, I'm wondering
whether some input entry in them may cause the problem. Have you tried
removing some of them to isolate the behavior?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.




--------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top