Hi ihechi.
Please refer below sample code.
HTML
<img class="img-responsive" style="margin: auto; width: 100%;" src="../images/Header.png" alt="" width="1177" height="168" usemap="#Map" />
<br />
<br />
<div style="margin-left: 300px;">
<asp:Label ID="lblTotal" runat="server" Style="font-size: 14px; font-weight: 600; color: firebrick;" Text="Label"></asp:Label><br />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true" PageSize="3"
OnPageIndexChanging="OnPageIndexChanging">
<Columns>
<asp:TemplateField HeaderText="Select All">
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="mailID" HeaderText="Id" ItemStyle-Width="30" />
<asp:BoundField DataField="FullName" HeaderText="Name" ItemStyle-Width="150" />
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:HyperLink ID="lnkEmail" runat="server" Text='<%# Eval("Email") %>' NavigateUrl='<%# Eval("Email", "mailto:{0}") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$("[id*=GridView1] [id*=chkAll]").on("click", function () {
//Get the reference of Header CheckBox.
var chkAll = $(this);
//Loop through all GridView CheckBoxes except Header CheckBox.
$("[id*=GridView1] [id*=chkSelect]").not("[id*=chkAll]").each(function () {
$(this)[0].checked = chkAll[0].checked;
});
});
</script>
<script type="text/javascript">
$("[id*=GridView1] [id*=chkSelect]").on("click", function () {
//Get the reference of Header CheckBox.
var chkAll = $("[id*=GridView1] [id*=chkAll]");
//Set Header CheckBox checked to true.
chkAll[0].checked = true;
//Loop through all GridView CheckBoxes except Header CheckBox.
$("[id*=GridView1] [id*=chkSelect]").not("[id*=chkAll]").each(function () {
if (!$(this).is(":checked")) {
chkAll[0].checked = false;
return;
}
});
});
</script>
<br />
<asp:Button Text="Send Email Reminder" runat="server" OnClick="SendReminderEmail" /><br />
<%-- <asp:Panel ID="pnlThankYouMessage" runat="server" Visible="False">
Email successfully sent
</asp:Panel>--%>
</div>
Namespace
C#
using System.Collections;
using System.Data;
using System.Threading.Tasks;
using System.Net.Mail;
VB.Net
Imports System.Collections
Imports System.Data
Imports System.Threading.Tasks
Imports System.Net.Mail
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
ArrayList checkboxArray;
if (ViewState["CheckBoxArray"] == null)
{
checkboxArray = new ArrayList();
}
else
{
checkboxArray = (ArrayList)ViewState["CheckBoxArray"];
}
if (this.IsPostBack)
{
int checkBoxIndex;
bool checkAllWasChecked = false;
CheckBox chkAll = (CheckBox)GridView1.HeaderRow.Cells[0].FindControl("chkAll");
string checkAllIndex = "chkAll-" + GridView1.PageIndex;
if (chkAll.Checked)
{
if (checkboxArray.IndexOf(checkAllIndex) == -1)
{
checkboxArray.Add(checkAllIndex);
}
}
else
{
if (checkboxArray.IndexOf(checkAllIndex) != -1)
{
checkboxArray.Remove(checkAllIndex);
checkAllWasChecked = true;
}
}
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (GridView1.Rows[i].RowType == DataControlRowType.DataRow)
{
CheckBox chk = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("chkSelect");
checkBoxIndex = GridView1.PageSize * GridView1.PageIndex + (i + 1);
if (chk.Checked)
{
if (checkboxArray.IndexOf(checkBoxIndex) == -1 && !checkAllWasChecked)
{
checkboxArray.Add(checkBoxIndex);
}
}
else
{
if (checkboxArray.IndexOf(checkBoxIndex) != -1 || checkAllWasChecked)
{
checkboxArray.Remove(checkBoxIndex);
}
}
}
}
}
ViewState["CheckBoxArray"] = checkboxArray;
GridView1.DataSource = LoadData();
GridView1.DataBind();
lblTotal.Text = GridView1.Rows.Count.ToString() + " members have yet to complete the form";
}
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
if (ViewState["CheckBoxArray"] != null)
{
ArrayList CheckBoxArray = (ArrayList)ViewState["CheckBoxArray"];
string checkAllIndex = "chkAll-" + GridView1.PageIndex;
if (CheckBoxArray.IndexOf(checkAllIndex) != -1)
{
CheckBox chkAll = (CheckBox)GridView1.HeaderRow.Cells[0].FindControl("chkAll");
chkAll.Checked = true;
}
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (GridView1.Rows[i].RowType == DataControlRowType.DataRow)
{
if (CheckBoxArray.IndexOf(checkAllIndex) != -1)
{
CheckBox chk = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("chkSelect");
chk.Checked = true;
}
else
{
int CheckBoxIndex = GridView1.PageSize * (GridView1.PageIndex) + (i + 1);
if (CheckBoxArray.IndexOf(CheckBoxIndex) != -1)
{
CheckBox chk = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("chkSelect");
chk.Checked = true;
}
}
}
}
}
}
protected DataTable LoadData()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("mailID", typeof(int)),
new DataColumn("FullName", typeof(string)),
new DataColumn("Email", typeof(string))
});
dt.Rows.Add("1001", "Nick Fury", "shield@gmail.com");
dt.Rows.Add("1002", "Robert DownleyJr", "ironman@gmail.com");
dt.Rows.Add("1003", "Andrew Garfield", "spidey@gmail.com");
dt.Rows.Add("1004", "Chris Evan", "captain@gmail.com");
dt.Rows.Add("1005", "Bruce Banner", "hulk@gmail.com");
dt.Rows.Add("1006", "Chris Hemsworth", "thor@gmail.com");
return dt;
}
protected void SendReminderEmail(object sender, EventArgs e)
{
// Create a temporary DataTable
DataTable dtCustomers = new DataTable();
dtCustomers.Columns.AddRange(new DataColumn[2] {
new DataColumn("fullName", typeof(string)),
new DataColumn("email", typeof(string))
});
GridView1.AllowPaging = false;
GridView1.DataBind();
// 'Copy the Checked Rows to DataTable
ArrayList checkboxArray = (ArrayList)ViewState["CheckBoxArray"];
int index = 1;
foreach (GridViewRow row in GridView1.Rows)
{
if (checkboxArray.Contains(index))
{
dtCustomers.Rows.Add(row.Cells[2].Text, (row.FindControl("lnkEmail") as HyperLink).Text);
}
index++;
}
GridView1.AllowPaging = true;
GridView1.DataBind();
string subject = "Completion Reminder";
string body = "Hello {0},<br /><br />Please remember to complete the form before deadline.<br /><br />Thanks.";
// Using Parallel Multi-Threading send multiple bulk email.
Parallel.ForEach(dtCustomers.AsEnumerable(), row =>
{
SendEmail(row["email"].ToString(), subject, string.Format(body, row["fullName"]));
});
}
private bool SendEmail(string recipient, string subject, string body)
{
MailMessage mm = new MailMessage();
mm.To.Add(recipient);
MailAddress EmailSender = new MailAddress("myemail@domain.com");
mm.From = EmailSender;
mm.Subject = subject;
mm.Body = body;
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
// Configure an SmtpClient to send the mail.
SmtpClient client = new SmtpClient("smtp.hostname.com");
client.EnableSsl = false;
client.Send(mm);
return true;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim checkboxArray As ArrayList
If ViewState("CheckBoxArray") Is Nothing Then
checkboxArray = New ArrayList()
Else
checkboxArray = CType(ViewState("CheckBoxArray"), ArrayList)
End If
If Me.IsPostBack Then
Dim checkBoxIndex As Integer
Dim checkAllWasChecked As Boolean = False
Dim chkAll As CheckBox = CType(GridView1.HeaderRow.Cells(0).FindControl("chkAll"), CheckBox)
Dim checkAllIndex As String = "chkAll-" & GridView1.PageIndex
If chkAll.Checked Then
If checkboxArray.IndexOf(checkAllIndex) = -1 Then
checkboxArray.Add(checkAllIndex)
End If
Else
If checkboxArray.IndexOf(checkAllIndex) <> -1 Then
checkboxArray.Remove(checkAllIndex)
checkAllWasChecked = True
End If
End If
For i As Integer = 0 To GridView1.Rows.Count - 1
If GridView1.Rows(i).RowType = DataControlRowType.DataRow Then
Dim chk As CheckBox = CType(GridView1.Rows(i).Cells(0).FindControl("chkSelect"), CheckBox)
checkBoxIndex = GridView1.PageSize * GridView1.PageIndex + (i + 1)
If chk.Checked Then
If checkboxArray.IndexOf(checkBoxIndex) = -1 AndAlso Not checkAllWasChecked Then
checkboxArray.Add(checkBoxIndex)
End If
Else
If checkboxArray.IndexOf(checkBoxIndex) <> -1 OrElse checkAllWasChecked Then
checkboxArray.Remove(checkBoxIndex)
End If
End If
End If
Next
End If
ViewState("CheckBoxArray") = checkboxArray
GridView1.DataSource = LoadData()
GridView1.DataBind()
lblTotal.Text = GridView1.Rows.Count.ToString() & " members have yet to complete the form"
End Sub
Protected Sub OnPageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
GridView1.PageIndex = e.NewPageIndex
GridView1.DataBind()
If ViewState("CheckBoxArray") IsNot Nothing Then
Dim CheckBoxArray As ArrayList = CType(ViewState("CheckBoxArray"), ArrayList)
Dim checkAllIndex As String = "chkAll-" & GridView1.PageIndex
If CheckBoxArray.IndexOf(checkAllIndex) <> -1 Then
Dim chkAll As CheckBox = CType(GridView1.HeaderRow.Cells(0).FindControl("chkAll"), CheckBox)
chkAll.Checked = True
End If
For i As Integer = 0 To GridView1.Rows.Count - 1
If GridView1.Rows(i).RowType = DataControlRowType.DataRow Then
If CheckBoxArray.IndexOf(checkAllIndex) <> -1 Then
Dim chk As CheckBox = CType(GridView1.Rows(i).Cells(0).FindControl("chkSelect"), CheckBox)
chk.Checked = True
Else
Dim CheckBoxIndex As Integer = GridView1.PageSize * (GridView1.PageIndex) + (i + 1)
If CheckBoxArray.IndexOf(CheckBoxIndex) <> -1 Then
Dim chk As CheckBox = CType(GridView1.Rows(i).Cells(0).FindControl("chkSelect"), CheckBox)
chk.Checked = True
End If
End If
End If
Next
End If
End Sub
Protected Function LoadData() As DataTable
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("mailID", GetType(Integer)), New DataColumn("FullName", GetType(String)), New DataColumn("Email", GetType(String))})
dt.Rows.Add("1001", "Nick Fury", "shield@gmail.com")
dt.Rows.Add("1002", "Robert DownleyJr", "ironman@gmail.com")
dt.Rows.Add("1003", "Andrew Garfield", "spidey@gmail.com")
dt.Rows.Add("1004", "Chris Evan", "captain@gmail.com")
dt.Rows.Add("1005", "Bruce Banner", "hulk@gmail.com")
dt.Rows.Add("1006", "Chris Hemsworth", "thor@gmail.com")
Return dt
End Function
Protected Sub SendReminderEmail(ByVal sender As Object, ByVal e As EventArgs)
Dim dtCustomers As DataTable = New DataTable()
dtCustomers.Columns.AddRange(New DataColumn(1) {New DataColumn("fullName", GetType(String)), New DataColumn("email", GetType(String))})
GridView1.AllowPaging = False
GridView1.DataBind()
Dim checkboxArray As ArrayList = CType(ViewState("CheckBoxArray"), ArrayList)
Dim index As Integer = 1
For Each row As GridViewRow In GridView1.Rows
If checkboxArray.Contains(index) Then
dtCustomers.Rows.Add(row.Cells(2).Text, (TryCast(row.FindControl("lnkEmail"), HyperLink)).Text)
End If
index += 1
Next
GridView1.AllowPaging = True
GridView1.DataBind()
Dim subject As String = "Completion Reminder"
Dim body As String = "Hello {0},<br /><br />Please remember to complete the form before deadline.<br /><br />Thanks."
Parallel.ForEach(dtCustomers.AsEnumerable(),
Function(row)
Return SendEmail(row("email").ToString(), subject, String.Format(body, row("FullName")))
End Function)
End Sub
Private Function SendEmail(ByVal recipient As String, ByVal subject As String, ByVal body As String) As Boolean
Dim mm As MailMessage = New MailMessage()
mm.[To].Add(recipient)
Dim EmailSender As MailAddress = New MailAddress("myemail@domain.com")
mm.From = EmailSender
mm.Subject = subject
mm.Body = body
mm.IsBodyHtml = True
Dim smtp As SmtpClient = New SmtpClient()
Dim client As SmtpClient = New SmtpClient("smtp.hostname.com")
client.EnableSsl = False
client.Send(mm)
Return True
End Function
Screenshot