将复选框放在列表视图中

时间:2014-10-26 04:34:54

标签: asp.net vb.net

我创建了一个表单,其中有一个listview,其中有一个来自数据库表的电子邮件ID。有一个按钮&点击按钮选中的特定电子邮件ID复选框应该发送邮件。请让我知道如何做到这一点。

这是我现在正在处理的代码。

ASPX代码

    <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代码

    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

在回答中修改为建议。

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("something@gmail.com", "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("something@gmail.com", "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

1 个答案:

答案 0 :(得分:2)

您需要修改aspx中的listview以在模板字段中包含checkbox。喜欢以下。

<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>

按下按钮,使用以下按钮,以获取所选复选框。

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("something@gmail.com", "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("something@gmail.com", "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