Menus

Saturday, January 19, 2013

Email only Checked row from GridView using ASP.Net



Below is the JQuery for Check / Uncheck

<script type="text/javascript">
        $(document).ready(function () {
            $('[id$=chkHeader]').click(function () {
                $("[id$='chkChild']").attr('checked', this.checked);
            });
        });
    </script>
And the Html code is

<asp:GridView ID="grdSearch" runat="server" AllowPaging="True" Width="700px"
                    AutoGenerateColumns="False" BackColor="White" BorderColor="Aqua"
                    DataKeyNames="MemberNO" Style="margin-top: 0px"
                    onpageindexchanging="grdSearch_PageIndexChanging">
                    <PagerSettings FirstPageText="First" LastPageText="Last" NextPageText="Next" PreviousPageText="Previous" Position="TopAndBottom" Mode="NextPreviousFirstLast" />
                    <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
                    <PagerStyle BorderColor="#00CCFF" HorizontalAlign="Center" />
                    <Columns>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                <asp:CheckBox ID="chkHeader" runat="server" />
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:CheckBox ID="chkChild" runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="customerid" HeaderText="Customer id" SortExpression="customerid" />
                        <asp:BoundField DataField="customername" HeaderText="Customer name" ItemStyle-HorizontalAlign="Left"
                            SortExpression="customername">
                            <ItemStyle HorizontalAlign="Left" />
                        </asp:BoundField>
                        <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
                        </Columns>
                </asp:GridView>
 

protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gr in grdSearch.Rows)
        {
            CheckBox chkEmail = ((CheckBox)gr.Cells[0].FindControl("ChkEmailSelection"));
            if (chkEmail.Checked == true)
            {
                var fromAddress = new MailAddress("username@domainname.com", "Admin");
                var toAddress = new MailAddress(gr.Cells[0].Text);
                const string fromPassword = "xxxxxx";
                string subject = Yoursubject.Text;
                string body = YourMessage.Text;

                var smtp = new SmtpClient
                {
                    Host = "smtp.yourdomainname.com",
                    Port = 587,
                    EnableSsl = true,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
                };
                using (var message = new MailMessage(fromAddress, toAddress)
                {
                    Subject = subject,
                    Body = body
                })
                {
                    smtp.Send(message);
                }
            }
        }
    }

No comments:

Post a Comment