Page.IsValid changes to false after button click

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Jocelyn

    Page.IsValid changes to false after button click

    Hi

    I have couple of dropdown controls each with a requiredfieldva lidator. 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(ob ject sender, EventArgs e)
    {
    ddl.ClearSelect ion();
    Trace.Write(Pag e.Validators[0].IsValid.ToStri ng()); // returns True
    }

    protected override void OnPreRender(Eve ntArgs e)
    {
    Trace.Write(Pag e.Validators[0].IsValid.ToStri ng()); // returns False
    base.OnPreRende r(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


  • =?Utf-8?B?YnJ1Y2UgYmFya2Vy?=

    #2
    RE: Page.IsValid changes to false after button click

    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)


    "Andrew Jocelyn" wrote:
    Hi
    >
    I have couple of dropdown controls each with a requiredfieldva lidator. 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(ob ject sender, EventArgs e)
    {
    ddl.ClearSelect ion();
    Trace.Write(Pag e.Validators[0].IsValid.ToStri ng()); // returns True
    }
    >
    protected override void OnPreRender(Eve ntArgs e)
    {
    Trace.Write(Pag e.Validators[0].IsValid.ToStri ng()); // returns False
    base.OnPreRende r(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
    >
    >
    >

    Comment

    • Andrew Jocelyn

      #3
      Re: Page.IsValid changes to false after button click

      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


      Comment

      • Steven Cheng [MSFT]

        #4
        Re: Page.IsValid changes to false after button click

        Hi Andrew,

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

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

        </div>
        <asp:DropDownLi st ID="DropDownLis t1" runat="server">
        <asp:ListItem ></asp:ListItem>
        <asp:ListItem>a aa</asp:ListItem>
        <asp:ListItem>b bb</asp:ListItem>
        <asp:ListItem>c cc</asp:ListItem>
        </asp:DropDownLis t>
        <asp:RequiredFi eldValidator ID="RequiredFie ldValidator1" runat="server"
        ControlToValida te="DropDownLis t1"
        ErrorMessage="R equiredFieldVal idator"></asp:RequiredFie ldValidator>
        <br />
        <br />
        <asp:DropDownLi st ID="DropDownLis t2" runat="server">
        <asp:ListItem ></asp:ListItem>
        <asp:ListItem>a aa</asp:ListItem>
        <asp:ListItem>b bb</asp:ListItem>
        <asp:ListItem>c cc</asp:ListItem>
        </asp:DropDownLis t>
        <asp:RequiredFi eldValidator ID="RequiredFie ldValidator2" runat="server"
        ControlToValida te="DropDownLis t2"
        ErrorMessage="R equiredFieldVal idator"></asp:RequiredFie ldValidator>
        <br />
        <br />
        <asp:DropDownLi st ID="DropDownLis t3" runat="server">
        <asp:ListItem ></asp:ListItem>
        <asp:ListItem>a aa</asp:ListItem>
        <asp:ListItem>b bb</asp:ListItem>
        <asp:ListItem>c cc</asp:ListItem>
        </asp:DropDownLis t>
        <asp:RequiredFi eldValidator ID="RequiredFie ldValidator3" runat="server"
        ControlToValida te="DropDownLis t3"
        ErrorMessage="R equiredFieldVal idator"></asp:RequiredFie ldValidator>
        <br />
        <br />
        <asp:Button ID="btnPost" runat="server" onclick="btnPos t_Click"
        Text="Post Button" />
        <asp:Button ID="btnClear" runat="server" onclick="btnCle ar_Click"
        Text="Clear Button" />
        </form>


        =-========code behind=======
        public partial class ValidatePage : System.Web.UI.P age
        {
        protected void Page_Load(objec t sender, EventArgs e)
        {

        }
        protected void btnPost_Click(o bject sender, EventArgs e)
        {

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

        DropDownList1.C learSelection() ;
        DropDownList2.C learSelection() ;
        DropDownList3.C learSelection() ;

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

        protected override void OnPreRender(Eve ntArgs e)
        {
        base.OnPreRende r(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:
        msdnmg@microsof t.com.

        =============== =============== =============== =====
        Get notification to my posts through email? Please refer to
        Gain technical skills through documentation and training, earn certifications and connect with the community

        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/subscripti...t/default.aspx.
        =============== =============== =============== =====
        This posting is provided "AS IS" with no warranties, and confers no rights.




        --------------------
        >From: "Andrew Jocelyn" <j055@newsgroup s.nospam>
        >References: <uPJHHg$mIHA.60 32@TK2MSFTNGP03 .phx.gbl>
        <6D954008-5C17-474E-BF47-37C0038671E6@mi crosoft.com>
        >Subject: Re: Page.IsValid changes to false after button click
        >Date: Sat, 12 Apr 2008 11:29:30 +0100
        >
        >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
        >
        >
        >

        Comment

        Working...