Hi Mehram,
You are using Update Panel. So you need to reapply the code on partial postback. Check the below updated code.
HTML
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
<style type="text/css">
.img-circle
{
border-radius: 50%;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript">
//On Page Load
$(function () {
SetSelection();
});
//On UpdatePanel Refresh
var prm = Sys.WebForms.PageRequestManager.getInstance();
if (prm != null) {
prm.add_endRequest(function (sender, e) {
if (sender._postBackSettings.panelsToUpdate != null) {
SetSelection();
}
});
};
function SetSelection() {
$('[id*=hfSelected]').val('');
$('[id*=lblImage]').on('click', function () {
if ($(this).css('opacity') != "0.5") {
$(this).css("opacity", 0.5);
$(this).closest('td').find('[id*=hfSelected]').val(1);
} else {
$(this).css("opacity", 1);
$(this).closest('td').find('[id*=hfSelected]').val(0);
}
});
}
</script>
<section class="content-header">
<h1>Voting </h1>
</section>
<asp:UpdatePanel ID="upContent" runat="server">
<ContentTemplate>
<section class="content">
<div class="row">
<div class="col-xs-12">
<div class="box-body table-responsive">
<div class="row">
<div class="col-xs-12">
<table class="table table-bordered">
<tr>
<th>Company</th>
<th>Election Year</th>
<th></th>
</tr>
<tr>
<td style="width: 200px;">
<asp:DropDownList ID="cboCompany" runat="server" CssClass="chzn-select ddst" Width="100%"></asp:DropDownList>
</td>
<td style="width: 380px;">
<asp:DropDownList ID="cboElectionYear" runat="server" CssClass="chzn-select ddst" Width="100%" Enabled="true"></asp:DropDownList>
</td>
<td style="text-align: left;">
<asp:LinkButton ID="btnGetReport" runat="server" CssClass="btn btn-default btn-flat toolbtn" Style="float: left; margin: 0px;">Get Data</asp:LinkButton>
</td>
</tr>
</table>
</div>
</div>
<br />
<div class="row" id="divOptions" runat="server" visible="false">
<div class="col-xs-12" style="text-align: center;">
<asp:LinkButton ID="lnkExcel" runat="server" CssClass="gridbuttonred" style="padding:4px; border-radius:0px" Visible="false">Export to Excel</asp:LinkButton>
</div>
</div>
<br />
<div class="row">
<div class="col-xs-12 col-lg-12 col-md-12 col- row">
<asp:DataList ID="dlCustomers" runat="server" RepeatColumns="4" CellSpacing="5" RepeatLayout="Table"
DataKeyField="ElectionContributorsID">
<ItemTemplate>
<table class="table" border="0">
<tr>
<th colspan="2"><b><%# Eval("MemberName") %></b></th>
</tr>
<tr>
<td colspan="2">
<asp:Image ID="lblImage" runat="server" CssClass="img-circle" Width="100px" hight="100px"
ImageUrl='<%#DataBinder.Eval(Container.DataItem, "PicPath")%>' />
<asp:HiddenField ID="hfSelected" runat="server" />
</td>
</tr>
<tr>
<td>Phone:</td>
<td><%# Eval("MemberFatherName")%></td>
</tr>
<tr>
<td>Fax:</td>
<td><%# Eval("FamilyName")%></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
<asp:Button ID="Button1" Text="Save" runat="server" OnClick="OnSave" />
</div>
</div>
</div>
</div>
</div>
</section>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="lnkExcel" />
</Triggers>
</asp:UpdatePanel>
<asp:UpdateProgress DisplayAfter="0" ID="MPG1" AssociatedUpdatePanelID="upContent"
runat="server">
<ProgressTemplate>
<div id="overlay">
<div id="modalprogress">
<div id="theprogress">
<img alt="indicator" src="Images/loading.gif" />
<br />
<br />
Please wait while system is updating ...
</div>
</div>
</div>
</ProgressTemplate>
</asp:UpdateProgress>
</asp:Content>
Code
Protected Sub OnSave(ByVal sender As Object, ByVal e As EventArgs)
Dim i As Integer = 0
For Each item As DataListItem In dlCustomers.Items
Dim isSelected As Boolean = If(TryCast(item.FindControl("hfSelected"), HiddenField).Value = "1", True, False)
If isSelected Then
Dim id As String = dlCustomers.DataKeys(item.ItemIndex).ToString()
Dim path As String = TryCast(item.FindControl("lblImage"), System.Web.UI.WebControls.Image).ImageUrl
i += Me.InsertDetails(id, path)
End If
Next
ScriptManager.RegisterClientScriptBlock(TryCast(sender, Control), Me.GetType(), "alert", "alert('" & i.ToString() & " records inserted.')", True)
End Sub
Private Function InsertDetails(ByVal id As String, ByVal path As String) As Integer
Dim i As Integer = 0
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As Data.SqlClient.SqlConnection = New Data.SqlClient.SqlConnection(conString)
Dim query As String = "INSERT INTO MemberDetails VALUES(@Id,@ImagePath)"
Dim cmd As Data.SqlClient.SqlCommand = New Data.SqlClient.SqlCommand(query, con)
cmd.Parameters.AddWithValue("@Id", id)
cmd.Parameters.AddWithValue("@ImagePath", path)
con.Open()
i = cmd.ExecuteNonQuery()
con.Close()
End Using
Return i
End Function