Skip to main content

Practical-13

Problem Statement

Write a program which demonstrates the Validation Control.

Solution

To demonstrate the Validation Control in ASP.NET, follow the steps below:

  1. Create a new ASP.NET Web Application project in Visual Studio.
  2. Add a new Web Form to the project by right-clicking on the project in the Solution Explorer, selecting "Add" > "Web Form".
  3. Rename the Web Form to "Default.aspx".
  4. Open the "Default.aspx" file and design the user interface as per your requirements using HTML and ASP.NET controls.
  5. Add the following controls to the "Default.aspx" form:
    • TextBox control: <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
    • RequiredFieldValidator control: <asp:RequiredFieldValidator ID="rfvName" runat="server" ControlToValidate="txtName" ErrorMessage="Name is required"></asp:RequiredFieldValidator>
    • Button control: <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"></asp:Button>
    • Label control: <asp:Label ID="lblMessage" runat="server"></asp:Label>
  6. In the code-behind file, write the following code in the btnSubmit_Click event handler to validate the input:
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
lblMessage.Text = "Validation successful!";
}
else
{
lblMessage.Text = "Validation failed!";
}
}
  1. Build and run the application to see the validation control in action.

This completes the solution for demonstrating the Validation Control in ASP.NET.