OnTextChanged control not firing when i scan the barcodes please help
<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/FormsV2/Site1.Master" CodeBehind="CandidatesSelection.aspx.vb" Inherits="SMIS2022WEB.CandidatesSelection" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<style type="text/css">
.auto-style3 {
width: 100%;
}
.auto-style4 {
width: 216px;
}
.auto-style5 {
width: 47px;
}
.auto-style6 {
width: 118px;
}
</style>
<script type="text/javascript">
window.onload = function () {
var txtBox = document.getElementById('<%= Studentaccount.ClientID %>');
if (txtBox) {
txtBox.focus();
}
};
</script>
<script type="text/javascript">
function focusOnFirstCheckbox() {
var checkboxes = document.querySelectorAll("#<%= dlCandidates.ClientID %> input[type='checkbox']");
if (checkboxes.length > 0) {
checkboxes[0].focus();
}
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<table class="auto-style3">
<tr>
<td class="auto-style6"><strong>Voter Number</strong></td>
<td class="auto-style4">
<asp:TextBox ID="Studentaccount" runat="server" Width="197px" OnTextChanged="Studentaccount_TextChanged" AutoPostBack="True"></asp:TextBox>
</td>
<td class="auto-style4">
<asp:Label ID="Name" runat="server" Text="Name"></asp:Label>
</td>
<td class="auto-style5">
<asp:Label ID="classr" runat="server" Text="Class"></asp:Label>
</td>
<td>
<asp:Label ID="Stream" runat="server" Text="Stream"></asp:Label>
</td>
<td>
<asp:TextBox ID="Accountb" runat="server"></asp:TextBox>
</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td colspan="8">
<asp:DataList ID="dlCandidates" runat="server" RepeatColumns="3" RepeatDirection="Horizontal" Width="322px">
<ItemTemplate>
<div class="candidate-box">
<asp:Image ID="imgCandidate" runat="server" ImageUrl='<%# Eval("PhotoPath") %>' Width="150px" Height="120px"/>
<br />
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
<br />
<asp:Label ID="lblPosition" runat="server" Text='<%# Eval("PositionName") %>'></asp:Label>
<br />
<asp:CheckBox ID="chkVote" runat="server" CommandArgument='<%# Eval("CandidateID") %>' />
</div>
<asp:HiddenField ID="hfCandidateID" runat="server" Value='<%# Eval("CandidateID") %>' />
<asp:HiddenField ID="hfGroupID" runat="server" Value='<%# Eval("GroupID") %>' />
</ItemTemplate>
</asp:DataList>
<asp:Button ID="btnSubmitVote" runat="server" Text="Submit Vote" OnClick="btnSubmitVote_Click"/>
<asp:Button ID="btnFirst" runat="server" OnClick="btnFirst_Click" Text="First" />
<asp:Button ID="btnPrev" runat="server" OnClick="btnPrev_Click" Text="Previous" />
<asp:Button ID="btnNext" runat="server" OnClick="btnNext_Click" Text="Next" />
<asp:Button ID="btnLast" runat="server" OnClick="btnLast_Click" Text="Last" />
</td>
</tr>
</table>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Studentaccount" EventName="TextChanged" />
</Triggers>
</asp:UpdatePanel>
</asp:Content>
This is not firing when i scanner the studentaccount using a barcode scanner
<td class="auto-style4">
<asp:TextBox ID="Studentaccount" runat="server" Width="197px" OnTextChanged="Studentaccount_TextChanged" AutoPostBack="True"></asp:TextBox>
</td>
Imports System.Data.SqlClient
Imports System.Configuration
Public Class CandidatesSelection
Inherits System.Web.UI.Page
Dim currentPage As Integer = 0
Dim holdhfCandidateID As Integer
Dim groupIDb As String
Dim hfCandidateIDbk, holdhfGroupID As HiddenField
Dim pageSize As Integer = 6 ' Adjust as needed
Dim constr As New SqlConnection(ConfigurationManager.ConnectionStrings("SMIS2022ConnectionString").ConnectionString)
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("SMIS2022ConnectionString").ConnectionString)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadCandidates()
' LoadCandidates2000()
End If
End Sub
Private Sub LoadCandidates2000()
Dim query As String = "SELECT c.CandidateID, s.StudentName, p.PositionName, c.Photo
FROM Candidates c
JOIN Students s ON c.StudentID = s.StudentID
JOIN Positions p ON c.PositionID = p.PositionID"
Dim cmd As New SqlCommand(query, constr)
Dim da As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
con.Open()
da.Fill(dt)
con.Close()
dlCandidates.DataSource = dt
dlCandidates.DataBind()
End Sub
Private Sub LoadCandidates()
Dim offset As Integer = currentPage * pageSize
Dim query As String = "SELECT dbo.RegisterGroups.PositionName, dbo.Candidates.Name, dbo.Candidates.PhotoPath, dbo.Candidates.CandidateID,dbo.Candidates.GroupID
FROM dbo.Candidates INNER JOIN
dbo.RegisterGroups ON dbo.Candidates.GroupID = dbo.RegisterGroups.GroupID ORDER BY dbo.Candidates.GroupID OFFSET @Offset ROWS FETCH NEXT @PageSize ROWS ONLY"
Dim cmd As New SqlCommand(query, constr)
cmd.Parameters.AddWithValue("@Offset", offset)
cmd.Parameters.AddWithValue("@PageSize", pageSize)
Dim dt As New DataTable()
Dim adapter As New SqlDataAdapter(cmd)
adapter.Fill(dt)
dlCandidates.DataSource = dt
dlCandidates.DataBind()
End Sub
Protected Sub chkVotes_CheckedChanged(sender As Object, e As EventArgs)
Dim chk As CheckBox = CType(sender, CheckBox)
Dim item As DataListItem = CType(chk.NamingContainer, DataListItem)
Dim hfCandidateID As HiddenField = CType(item.FindControl("hfCandidateID"), HiddenField)
Dim hfGroupID As HiddenField = CType(item.FindControl("hfGroupID"), HiddenField)
Dim candidateID As Integer = Convert.ToInt32(hfCandidateID.Value)
Dim groupID As Integer = Convert.ToInt32(hfGroupID.Value)
' Uncheck other candidates in the same group
For Each listItem As DataListItem In dlCandidates.Items
Dim otherHfGroupID As HiddenField = CType(listItem.FindControl("hfGroupID"), HiddenField)
Dim otherChk As CheckBox = CType(listItem.FindControl("chkVote"), CheckBox)
If Convert.ToInt32(otherHfGroupID.Value) = groupID AndAlso listItem.ItemIndex <> item.ItemIndex Then
otherChk.Checked = False
End If
Next
' Save vote in the database
SaveVote(candidateID, groupID)
End Sub
Private Sub SaveVote(candidateID As Integer, groupID As Integer)
Dim deleteQuery As String = "DELETE FROM Votes WHERE GroupID = @GroupID AND StudentID = @StudentID"
Dim insertQuery As String = "INSERT INTO Votes (StudentID, CandidateID, GroupID) VALUES (@StudentID, @CandidateID, @GroupID)"
Using cmd As New SqlCommand(deleteQuery, con)
cmd.Parameters.AddWithValue("@GroupID", groupID)
cmd.Parameters.AddWithValue("@StudentID", Session("StudentID"))
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
Using cmd As New SqlCommand(insertQuery, con)
cmd.Parameters.AddWithValue("@StudentID", Session("StudentID"))
cmd.Parameters.AddWithValue("@CandidateID", candidateID)
cmd.Parameters.AddWithValue("@GroupID", groupID)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Sub
' Navigation Buttons
Protected Sub btnNext_Click(sender As Object, e As EventArgs)
currentPage += 1
LoadCandidates()
End Sub
Protected Sub btnPrev_Click(sender As Object, e As EventArgs)
If currentPage > 0 Then
currentPage -= 1
End If
LoadCandidates()
End Sub
Protected Sub btnFirst_Click(sender As Object, e As EventArgs)
currentPage = 0
LoadCandidates()
End Sub
Protected Sub btnLast_Click(sender As Object, e As EventArgs)
Dim countQuery As String = "SELECT COUNT(*) FROM Candidates"
Dim cmd As New SqlCommand(countQuery, con)
con.Open()
Dim totalRecords As Integer = Convert.ToInt32(cmd.ExecuteScalar())
con.Close()
currentPage = (totalRecords \ pageSize)
LoadCandidates()
End Sub
Protected Sub chkVote_CheckedChangedok(ByVal sender As Object, ByVal e As EventArgs)
Dim chk As CheckBox = CType(sender, CheckBox)
Dim item As DataListItem = CType(chk.NamingContainer, DataListItem)
' Dim candidateID As Integer = Convert.ToInt32(chk.CommandArgument)
' Get the position of the selected candidate
' Dim positionID As Integer = GetPositionID(candidateID)
' Dim studentID As Integer = GetCurrentStudentID()
' Check if the student has already voted for this position
' If HasVoted(studentID, positionID) Then
' Update the vote to the new candidate
'UpdateVote(studentID, candidateID, positionID)
' Else
' Insert new vote
'InsertVote(studentID, candidateID, positionID)
' End If
' Refresh DataList to reflect changes
' LoadCandidates()
End Sub
Private Function GetPositionID(ByVal candidateID As Integer) As Integer
Dim query As String = "SELECT GroupID FROM Candidates WHERE CandidateID = @CandidateID"
Dim cmd As New SqlCommand(query, con)
cmd.Parameters.AddWithValue("@CandidateID", candidateID)
con.Open()
Dim positionID As Object = cmd.ExecuteScalar()
con.Close()
Return Convert.ToInt32(positionID)
End Function
Private Function GetCurrentStudentID() As String
' Replace this with the actual way you fetch the logged-in student's ID
Return Session("StudentID")
End Function
Private Function HasVoted(ByVal studentID As Integer, ByVal positionID As Integer) As Boolean
Dim query As String = "SELECT COUNT(*) FROM Votes WHERE StudentID = @StudentID AND PositionID = @PositionID"
Dim cmd As New SqlCommand(query, con)
cmd.Parameters.AddWithValue("@StudentID", studentID)
cmd.Parameters.AddWithValue("@PositionID", positionID)
con.Open()
Dim count As Integer = Convert.ToInt32(cmd.ExecuteScalar())
con.Close()
Return count > 0
End Function
Private Sub UpdateVote(ByVal studentID As Integer, ByVal candidateID As Integer, ByVal positionID As Integer)
Dim query As String = "UPDATE Votes SET CandidateID = @CandidateID WHERE StudentID = @StudentID AND PositionID = @PositionID"
Dim cmd As New SqlCommand(query, con)
cmd.Parameters.AddWithValue("@StudentID", studentID)
cmd.Parameters.AddWithValue("@CandidateID", candidateID)
cmd.Parameters.AddWithValue("@PositionID", positionID)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Sub
Private Sub InsertVote(ByVal studentID As Integer, ByVal candidateID As Integer, ByVal positionID As Integer)
Dim query As String = "INSERT INTO Votes (StudentID, CandidateID, PositionID) VALUES (@StudentID, @CandidateID, @PositionID)"
Dim cmd As New SqlCommand(query, con)
cmd.Parameters.AddWithValue("@StudentID", studentID)
cmd.Parameters.AddWithValue("@CandidateID", candidateID)
cmd.Parameters.AddWithValue("@PositionID", positionID)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Sub
Protected Sub chkVote_CheckedChanged(sender As Object, e As EventArgs)
Dim chk As CheckBox = CType(sender, CheckBox)
Dim item As DataListItem = CType(chk.NamingContainer, DataListItem)
Dim hfCandidateID As HiddenField = CType(item.FindControl("hfCandidateID"), HiddenField)
Dim hfGroupID As HiddenField = CType(item.FindControl("hfGroupID"), HiddenField)
Dim candidateID As Integer = Convert.ToInt32(hfCandidateID.Value)
Dim groupID As Integer = Convert.ToInt32(hfGroupID.Value)
' Uncheck other candidates in the same group
For Each listItem As DataListItem In dlCandidates.Items
Dim otherHfGroupID As HiddenField = CType(listItem.FindControl("hfGroupID"), HiddenField)
Dim otherChk As CheckBox = CType(listItem.FindControl("chkVote"), CheckBox)
If Convert.ToInt32(otherHfGroupID.Value) = groupID AndAlso listItem.ItemIndex <> item.ItemIndex Then
otherChk.Checked = False
End If
Next
' Save vote in the database
SaveVote(candidateID, groupID)
End Sub
Protected Sub Studentaccount_TextChanged(sender As Object, e As EventArgs) Handles Studentaccount.TextChanged
GetAccountDetails()
Accountb.Focus()
UpdatePanel1.Update()
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "SetFocus", "focusOnFirstCheckbox();", True)
End Sub
Protected Sub GetAccountDetails()
Dim constrd As String = ConfigurationManager.ConnectionStrings("SMIS2022ConnectionString").ConnectionString
Using Con As New SqlConnection(constrd)
Con.Open()
Using Com2 As New SqlCommand("SELECT admno, Name, Class,Stream FROM P3P7 WHERE admno = '" & Studentaccount.Text & "'", Con)
Using RDR2 = Com2.ExecuteReader()
If RDR2.HasRows Then
RDR2.Read()
Studentaccount.Text = RDR2.Item("admno").ToString()
Name.Text = RDR2.Item("Name").ToString()
Stream.Text = RDR2.Item("Stream").ToString()
classr.Text = RDR2.Item("Class").ToString()
Session("StudentID") = RDR2.Item("admno").ToString()
' LoadCandidates()
'Studentaccount.Focus()
Exit Sub
Else
Dim time4 As String = "Voter Number doesnot exist"
ScriptManager.RegisterStartupScript(Me, [GetType](), "showalert", "alert('" & time4 & "');", True)
Studentaccount.Text = ""
Studentaccount.Focus()
Exit Sub
End If
Con.Close()
End Using
End Using
End Using
End Sub
Protected Sub btnSubmitVote_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmitVote.Click
If String.IsNullOrEmpty(Studentaccount.Text) Then
ScriptManager.RegisterStartupScript(Me, [GetType](), "showalert", "alert('You can Must indicate Student Voter Number');", True)
Studentaccount.Focus()
Exit Sub
Else
Dim studentID As String = Session("StudentID").ToString ' Replace with actual student ID from session or authentication
Dim studentID2 As String = Session("StudentID").ToString
Dim votes As New List(Of Tuple(Of Integer, Integer)) ' List of (CandidateID, GroupID)
For Each item As DataListItem In dlCandidates.Items
Dim chkVote As CheckBox = CType(item.FindControl("chkVote"), CheckBox)
Dim hfCandidateID As HiddenField = CType(item.FindControl("hfCandidateID"), HiddenField)
hfCandidateIDbk = CType(item.FindControl("hfCandidateID"), HiddenField)
Dim hfGroupID As HiddenField = CType(item.FindControl("hfGroupID"), HiddenField)
groupIDb = hfCandidateID.Value
holdhfGroupID = CType(item.FindControl("hfGroupID"), HiddenField)
If chkVote IsNot Nothing AndAlso chkVote.Checked Then
Dim candidateID As Integer = Convert.ToInt32(hfCandidateID.Value)
Dim holdhfCandidateID As Integer = Convert.ToInt32(hfCandidateID.Value)
Dim groupID As Integer = Convert.ToInt32(hfGroupID.Value)
votes.Add(New Tuple(Of Integer, Integer)(candidateID, groupID))
End If
Next
Dim positionIDk As Integer = GetPositionID(groupIDb)
Dim studentIDt As String = GetCurrentStudentID()
If HasVoted(studentIDt, positionIDk) Then
' Update the vote to the new candidate
' UpdateVote(studentID, candidateID, positionID)
ScriptManager.RegisterStartupScript(Me, [GetType](), "showalert", "alert('You can only vote for one candidate per group');", True)
Else
SaveVotes(studentID, votes)
VoteHandler.UpdateVote(groupIDb)
ScriptManager.RegisterStartupScript(Me, Me.[GetType](), "showalert", "alert('Vote submitted successfully');window.location ='" + Request.Url.AbsoluteUri + "';", True)
' Insert new vote
' InsertVote(studentID, candidateID, positionID)
End If
' Validate and save votes
End If
End Sub
Private Function GetGroupID(candidateID As Integer) As Integer
Dim connStr As String = ConfigurationManager.ConnectionStrings("SMIS2022ConnectionString").ConnectionString
Using con As New SqlConnection(connStr)
Dim query As String = "SELECT GroupID FROM Candidates WHERE CandidateID=@CandidateID"
Dim cmd As New SqlCommand(query, con)
cmd.Parameters.AddWithValue("@CandidateID", candidateID)
con.Open()
Dim groupID As Object = cmd.ExecuteScalar()
con.Close()
Return If(groupID IsNot Nothing, Convert.ToInt32(groupID), 0)
End Using
End Function
Private Function ValidateVotes(studentID As String, votes As List(Of Tuple(Of Integer, Integer))) As Boolean
Dim groupSet As New HashSet(Of Integer)()
For Each vote In votes
If groupSet.Contains(vote.Item2) Then
Return False ' Duplicate group vote detected
End If
groupSet.Add(vote.Item2)
Next
Return True
End Function
Private Function HasVoted(ByVal studentID As String, ByVal positionID As Integer) As Boolean
Dim query As String = "SELECT COUNT(*) FROM Votes WHERE StudentID = @StudentID AND GroupID = @PositionID"
Dim cmd As New SqlCommand(query, con)
cmd.Parameters.AddWithValue("@StudentID", studentID)
cmd.Parameters.AddWithValue("@PositionID", positionID)
con.Open()
Dim count As Integer = Convert.ToInt32(cmd.ExecuteScalar())
con.Close()
Return count > 0
End Function
Private Sub SaveVotes(studentID As String, votes As List(Of Tuple(Of Integer, Integer)))
Dim constrd As String = ConfigurationManager.ConnectionStrings("SMIS2022ConnectionString").ConnectionString
Using con As New SqlConnection(constrd)
con.Open()
For Each vote In votes
Dim query As String = "INSERT INTO Votes (StudentID, CandidateID, GroupID) VALUES (@StudentID, @CandidateID, @GroupID)"
Using cmd As New SqlCommand(query, con)
cmd.Parameters.AddWithValue("@StudentID", studentID)
cmd.Parameters.AddWithValue("@CandidateID", vote.Item1)
cmd.Parameters.AddWithValue("@GroupID", vote.Item2)
cmd.ExecuteNonQuery()
End Using
Next
con.Close()
End Using
End Sub
End Class