ARTICLE

Page.IsValid property in VB.NET

Posted by Rohatash Kumar Articles | ASP.NET using VB.NET July 22, 2011
Here, we will see how to use Page.IsValid property in VB.NET.
 
Reader Level:

Here, we see how to use Page.IsValid property in VB.NET. ASP.net ships with a couple of validator controls that allow you to determine whether the value of the input controls they are validating is valid. The Page.IsValid property tells you indicating whether page validation succeeded. True if page validation succeeded otherwise false.

Example

If the property returns true, the Text property of the Output control is set to "Page is valid!" Otherwise, it is set to "Some of the required fields are empty.

Page.Validate()
If (Page.IsValid) Then
lblOutput.Text = "Page is Valid!"
Else
lblOutput.Text = "Some required fields are empty."
End If


It can be called only after the Page.Validate method is called. By using this property, you can add logic to your page to determine whether to proceed with the PostBack event or not. So, in addition to relying on client side validation, it is also important that you call Page.IsValid when handling the postback event.

For Example

In this example we will see what happened when we click on the Button2.

<body>

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

    <div>

        <asp:TextBox ID="TextBox1" runat="server" ValidationGroup="MyValidationGroup"></asp:TextBox>

        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"

            ErrorMessage="This

 

field is required!" ValidationGroup="MyValidationGroup"></asp:RequiredFieldValidator>

        <br />

        <asp:Button ID="Button1" runat="server" Text="Button1" ValidationGroup="MyValidationGroup"

            CausesValidation="true" />

        <br />

        <br />

        <asp:TextBox ID="TextBox2" runat="server" ValidationGroup="AnotherValidationGroup"></asp:TextBox>

        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2"

            ErrorMessage="This

 

field is required!" ValidationGroup="AnotherValidationGroup"></asp:RequiredFieldValidator>

        <br />

        <asp:Button ID="Button2" runat="server" Text="Button2" ValidationGroup="AnotherValidationGroup"

            OnClick="Button2_Click" />

    </div>

    </form>

</body>

Now double click on the Button2 and add the following code.

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs)

        Page.Validate("MyValidationGroup")

        If Not Page.IsValid Then

            Return

        End If

        Response.Write("Button was clicked at " & DateTime.Now.ToShortTimeString())

    End Sub

 

Now run the application and enter some value in TextBoxes.

text box with validation

Figure1

Now click on the Button2.

Show is.valid property

Figure2

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor