Reputation:
I have made a form in which there is a listview in which there is a email id which comes from database tables. There is a button & on click of button selected checkboxes of particular email id that mail should be sent. Please let me know how to do that.
This is my code on which i am working now.
ASPX Code
<div id="send_request_form" runat="server" class="form2">
<div style="width:100%; padding:20px 0 20px 0; color:#fff; background:#3d76b0; font-weight:bold; text-align:center; font-size:24px; font-family:Calibri">Select Companies</div>
<asp:ListView ID="lvCustomers" runat="server" GroupPlaceholderID="groupPlaceHolder1"
ItemPlaceholderID="itemPlaceHolder1">
<LayoutTemplate>
<table class="client_list" cellpadding="0" cellspacing="0">
<tr>
<td style="padding:10px; text-align:center; width:15%; font-size:16px; font-weight:bold; background:#c4dbf2; color:#fff">
Id
</td>
<td style="padding:10px; text-align:center; width:60%; font-size:16px; font-weight:bold; background:#c4dbf2; color:#fff">
Company Name
</td>
<td style="padding:10px; text-align:center; width:25%; font-size:16px; font-weight:bold; background:#c4dbf2; color:#fff">
Email
</td >
</tr>
<asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
<tr>
<td colspan = "3">
<asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvCustomers" PageSize="20">
<Fields>
<asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="true"
ShowNextPageButton="false" />
<asp:NumericPagerField ButtonType="Link" Visible="false" />
<asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false" ShowPreviousPageButton = "false" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
<GroupTemplate>
<tr>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
</tr>
</GroupTemplate>
<ItemTemplate>
<td>
<%# Eval("Comp_ID")%>
</td>
<td>
<%# Eval("client_name")%>
</td>
<td>
<%# Eval("email_id")%>
</td>
</ItemTemplate>
</asp:ListView>
<asp:CheckBox ID="CheckBox1" runat="server" />
<asp:Button ID="sending" runat="server" Text="Send Request" />
</div>
</div>
VB Code
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindListView()
End If
add_client.Visible = False
send_request_form.Visible = False
headings.Text = "Dashboard"
End Sub
Private Sub BindListView()
Dim con As New SqlConnection("Data Source=SuRaj;Initial Catalog=Brandstik2; Integrated Security=True")
Using cmd As New SqlCommand()
cmd.CommandText = "SELECT Comp_ID, client_name, email_id FROM BrandstikTesti"
cmd.Connection = con
Using sda As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
sda.Fill(dt)
lvCustomers.DataSource = dt
lvCustomers.DataBind()
End Using
End Using
End Sub
Edits As Suggested in answer.
Protected Sub sending_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles sending.Click
Dim emails As New List(Of String)()
For Each item As ListViewItem In lvCustomers.Items
Dim ck As CheckBox = DirectCast(item.FindControl("CheckBox1"), CheckBox)
If ck.Checked Then
Try
Dim mail As New MailMessage()
Dim SmtpServer As New SmtpClient()
mail.Bcc.Add(lbl.text)
mail.From = New MailAddress("[email protected]", "SuRaj_ Email Test")
mail.Subject = "Email Testing"
mail.Body = "http://www.something.com/feedback.aspx?" +
mail.IsBodyHtml = True
SmtpServer.Port = 25
SmtpServer.Credentials = New System.Net.NetworkCredential("[email protected]", "123")
SmtpServer.Host = "smtp.gmail.com"
SmtpServer.EnableSsl = True
SmtpServer.Send(mail)
MsgBox("Mail Sent")
Catch error_t As Exception
MsgBox(error_t.ToString)
End Try
emails.Add(DirectCast(item.FindControl("lbl"), Label).Text)
End If
Next
End Sub
Upvotes: 1
Views: 1444
Reputation: 7462
You need to modify the listview
in aspx to have checkbox
inside template field. Like following.
<asp:ListView ID="lvCustomers" runat="server" GroupPlaceholderID="groupPlaceHolder1"
ItemPlaceholderID="itemPlaceHolder1">
<LayoutTemplate>
<table class="client_list" cellpadding="0" cellspacing="0">
<tr>
<td style="padding: 10px; text-align: center; width: 15%; font-size: 16px; font-weight: bold; background: #c4dbf2; color: #fff">Id
</td>
<td style="padding: 10px; text-align: center; width: 60%; font-size: 16px; font-weight: bold; background: #c4dbf2; color: #fff">Company Name
</td>
<td style="padding: 10px; text-align: center; width: 25%; font-size: 16px; font-weight: bold; background: #c4dbf2; color: #fff">Email
</td>
<td style="padding: 10px; text-align: center; width: 25%; font-size: 16px; font-weight: bold; background: #c4dbf2; color: #fff">Select one
</td>
</tr>
<asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
<tr>
<td colspan="3">
<asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvCustomers" PageSize="20">
<Fields>
<asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="true"
ShowNextPageButton="false" />
<asp:NumericPagerField ButtonType="Link" Visible="false" />
<asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false" ShowPreviousPageButton="false" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
<GroupTemplate>
<tr>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
</tr>
</GroupTemplate>
<ItemTemplate>
<td>
<%# Eval("Comp_ID")%>
</td>
<td>
<%# Eval("client_name")%>
</td>
<td>
<asp:Label ID="lbl" runat="server" Text='<%# Eval("email_id")%>' ></asp:Label>
</td>
<td>
<asp:CheckBox ID="CheckBox1" runat="server" />
</td>
</ItemTemplate>
</asp:ListView>
And it button click, use following, to get selected checkbox.
Protected Sub sending_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles sending.Click
For Each item As ListViewItem In lvCustomers.Items
Dim ck As CheckBox = DirectCast(item.FindControl("CheckBox1"), CheckBox)
If ck.Checked Then
Try
Dim mail As New MailMessage()
Dim SmtpServer As New SmtpClient()
mail.Bcc.Add(DirectCast(item.FindControl("lbl"), Label).Text)
mail.From = New MailAddress("[email protected]", "SuRaj_ Email Test")
mail.Subject = "Email Testing"
mail.Body = "http://www.something.com/feedback.aspx?" +
mail.IsBodyHtml = True
SmtpServer.Port = 25
SmtpServer.Credentials = New System.Net.NetworkCredential("[email protected]", "123")
SmtpServer.Host = "smtp.gmail.com"
SmtpServer.EnableSsl = True
SmtpServer.Send(mail)
MsgBox("Mail Sent")
Catch error_t As Exception
MsgBox(error_t.ToString)
End Try
End If
Next
End Sub
Upvotes: 2