[Solved] Login Button not working in ASP.Net

on Jul 26, 2022 01:30 AM

Please help login button not working when i pressed despite setting it

please help

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Login.aspx.vb" Inherits="SMIS2022WEB.Login1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <form id="form1" runat="server">
        <asp:Panel ID="Panel1" runat="server" Height="230px" style="z-index: 1; top: 16px; position: absolute; height: 230px; width: 280px; left: 355px">
            <table class="auto-style1">
                    <td class="auto-style6"><strong>Login</strong></td>
                    <td class="auto-style5"><strong>
                        <asp:TextBox ID="Usernamed" runat="server" Width="166px"></asp:TextBox>
                    <td class="auto-style5">
                        <asp:Label ID="FailureText" runat="server"></asp:Label>
                    <td class="auto-style4"><strong>Password</strong></td>
                    <td class="auto-style8"><strong>
                        <asp:TextBox ID="Password" runat="server" Width="160px"></asp:TextBox>
                    <td class="auto-style7"></td>
                    <td class="auto-style2"></td>
                    <td class="auto-style9">
                        <asp:Panel ID="Panel2" runat="server" CssClass="auto-style5" Height="35px" Width="163px">
                            <asp:Button ID="Button2" runat="server" OnAuthenticate="ValidateUser" style="text-align: center; z-index: 1; top: 79px; position: absolute; width: 55px; left: 135px;" Text="Login" />
                    <td class="auto-style9"></td>


Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Public Class Login1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            If Me.Page.User.Identity.IsAuthenticated Then

            End If
        End If
    End Sub

    Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    End Sub

    Protected Sub ValidateUser(sender As Object, e As EventArgs)
        Dim userId As Integer = 0
        Dim roles As String = String.Empty
        Dim constr As String = ConfigurationManager.ConnectionStrings("SMIS2022ConnectionString").ConnectionString
        Using con As New SqlConnection(constr)
            Using cmd As New SqlCommand("Validate_User")
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.AddWithValue("@Username", Usernamed.Text)
                cmd.Parameters.AddWithValue("@Password", Password.Text)
                cmd.Connection = con
                Dim reader As SqlDataReader = cmd.ExecuteReader()
                userId = Convert.ToInt32(reader("UserId"))
                roles = reader("Roles").ToString()
            End Using
            Select Case userId
                Case -1
                    FailureText.Text = "Username and/or password is incorrect."
                    Exit Select
                Case -2
                    FailureText.Text = "Account has not been activated."
                    Exit Select
                Case Else
                    '  Dim ticket As New FormsAuthenticationTicket(1, Usernamed.Text, DateTime.Now, DateTime.Now.AddMinutes(2880), Login1.RememberMeSet, roles,
                    ' FormsAuthentication.FormsCookiePath)
                    '  Dim hash As String = FormsAuthentication.Encrypt(ticket)
                    ' Dim cookie As New HttpCookie(FormsAuthentication.FormsCookieName, hash)

                    '  If ticket.IsPersistent Then
                    ' cookie.Expires = ticket.Expiration
                    '  End If
                    '  Response.Cookies.Add(cookie)
                    ' Response.Redirect(FormsAuthentication.GetRedirectUrl(Usernamed.Text, Login1.RememberMeSet))
                    Exit Select
            End Select
        End Using
    End Sub
End Class
Download FREE API for Word, Excel and PDF in ASP.Net: Download
on Jul 26, 2022 01:29 AM
makumbi says:
<asp:Button ID="Button2" runat="server" OnAuthenticate="ValidateUser" style="text-align: center; z-index: 1; top: 79px; position: absolute; width: 55px; left: 135px;" Text="Login" />

OnAuthenticate is availabel for ASP.Net Login control, not for Button control.

You need to write the code to Validate login inside the Button Click event i.e. Button2_Click.

After successfully validate you need to redirect to the desired page.

Refer below article for more details.

Responsive Login Form inside Bootstrap Modal Popup in ASP.Net