Write an ASP.Net program to create a user control that receives the user name and password from the user and validates them. If the user name is ‘DYP’ and the password is ‘Pimpri’, then the user is authorized, otherwise not.

Answer:

LoginControl.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="LoginControl.ascx.cs" Inherits="YourNamespace.LoginControl" %>

<div>
    <label for="txtUsername">Username:</label>
    <asp:TextBox ID="txtUsername" runat="server" />

    <br />

    <label for="txtPassword">Password:</label>
    <asp:TextBox ID="txtPassword" runat="server" TextMode="Password" />

    <br />

    <asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" />

    <br />

    <asp:Label ID="lblMessage" runat="server" ForeColor="Red"></asp:Label>
</div>

Code-Behind Logic (LogicControl.ascx.cs):

using System;

namespace YourNamespace
{
    public partial class LoginControl : System.Web.UI.UserControl
    {
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string username = txtUsername.Text;
            string password = txtPassword.Text;

            // Validate the credentials
            if (username == "DYP" && password == "Pimpri")
            {
                lblMessage.Text = "Welcome, authorized user!";
                lblMessage.ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                lblMessage.Text = "Invalid username or password.";
            }
        }
    }
}

Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="YourNamespace._Default" %>

<%@ Register Src="~/LoginControl.ascx" TagName="LoginControl" TagPrefix="uc" %>

<!DOCTYPE html>
<html>
<head runat="server">
    <title>Login Example</title>
</head>
<body>
    <form id="form1" runat="server">
        <uc:LoginControl ID="LoginControl1" runat="server" />
    </form>
</body>
</html>
Scroll to Top