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:
- Create a new ASP.NET Web Application project in Visual Studio.
- Add a new Web Form to the project by right-clicking on the project in the Solution Explorer, selecting "Add" > "Web Form".
- Rename the Web Form to "Default.aspx".
- Open the "Default.aspx" file and design the user interface as per your requirements using HTML and ASP.NET controls.
- 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>
- TextBox control:
- 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!";
}
}
- Build and run the application to see the validation control in action.
This completes the solution for demonstrating the Validation Control in ASP.NET.