ASP.NET Page.IsValid in VB.NET

Here, we will see how to use Page.IsValid property in VB.NET.
  • 4973

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 ObjectByVal 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. 
 

is1.gif
 

Figure1

Now click on the Button2.

is2.gif
 

Figure2

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.