Gridview select email

Gridview select email Here I will select multiple rows of gridview containg email addresses to send an email.

The complete source can be downloded from here:
Gridview_send_email.zip (4.02 kb)


Here is .aspx source code:
         <asp:TextBox ID="txtSubject" runat="server"></asp:TextBox><br />
        <br />
        <table>
            <tr>
                <td style="width: 280px">
                    Email body:</td>
                <td style="width: 163px">
                    Select Recipients</td>
            </tr>
            <tr>
                <td style="width: 280px" valign="top">
                    <asp:TextBox ID="TextBox1" runat="server" Height="184px" TextMode="MultiLine" Width="368px"></asp:TextBox>
                    <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="send" /></td>
                <td style="width: 163px">
                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
                        ForeColor="#333333" GridLines="None" DataKeyNames="Email">
                        <Columns>
                            <asp:TemplateField HeaderText="Sr.No.">
                                <ItemTemplate>
                                    <%# Container.DataItemIndex+1 %>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="First Name">
                                <ItemTemplate>
                                    &nbsp;<asp:Label ID="lblFirstName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"FirstName") %>'></asp:Label><br />
                                    <asp:TextBox ID="txtFirstName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"FirstName") %>'
                                        Visible="False"></asp:TextBox>
                                </ItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
                                </FooterTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Last Name">
                                <ItemTemplate>
                                    <asp:Label ID="lblLastName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"LastName") %>'></asp:Label>
                                    <br />
                                    <asp:TextBox ID="txtLastName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"LastName") %>'
                                        Visible="False"></asp:TextBox>
                                </ItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
                                </FooterTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtLastName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"LastName") %>'></asp:TextBox>
                                </EditItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Email">
                                <ItemTemplate>
                                    <asp:Label ID="lblEmail" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Email") %>'></asp:Label>
                                    <asp:TextBox ID="txtEmail" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Email") %>'
                                        Visible="False"></asp:TextBox>
                                </ItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
                                </FooterTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Check All">
                                <HeaderTemplate>
                                    <asp:CheckBox ID="chkAll" runat="server" AutoPostBack="True" OnCheckedChanged="chkAll_CheckedChanged" />
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:CheckBox ID="chkItem" runat="server" />
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                        <AlternatingRowStyle BackColor="White" />
                    </asp:GridView>
                </td>
            </tr>
        </table>

C#.Net code behind

GridView1.DataKeys[gr.RowIndex]["Email"].ToString()

For using DataKeys you need to add dataKeys to your gridview. you can add multiple dataKeys to your gridview separated by ",".

e.g.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
                        ForeColor="#333333" GridLines="None" DataKeyNames="Email">


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Net.Mail;
using System.Text;
using System.Net;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            BindGrid();
        }
    }

    private void BindGrid()
    {
        GridView1.DataSource = GridDataProvider.GetData();

        GridView1.DataBind();

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        GridView1.ShowFooter = true;
        BindGrid();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gr in GridView1.Rows)
        {
            TextBox t = (TextBox)gr.FindControl("txtFirstName");
            TextBox tLastName = (TextBox)gr.FindControl("txtLastName");
            TextBox tEmail = (TextBox)gr.FindControl("txtEmail");
            Label lFirstName = (Label)gr.FindControl("lblFirstName");
            Label lLastName = (Label)gr.FindControl("lblLastName");
            Label lEmail = (Label)gr.FindControl("lblEmail");

            if (t.Visible)
            {
                t.Visible = false;
                tLastName.Visible = false;
                tEmail.Visible = false;
                lFirstName.Visible = true;
                lLastName.Visible = true;
                lEmail.Visible = true;
            }
            else
            {
                t.Visible = true;
                tLastName.Visible = true;
                tEmail.Visible = true;
                lFirstName.Visible = false;
                lLastName.Visible = false;
                lEmail.Visible = false;

            }


        }
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gr in GridView1.Rows)
        {
            TextBox t = (TextBox)gr.FindControl("txtFirstName");
            TextBox tLastName = (TextBox)gr.FindControl("txtLastName");
            TextBox tEmail = (TextBox)gr.FindControl("txtEmail");
            Label lFirstName = (Label)gr.FindControl("lblFirstName");
            Label lLastName = (Label)gr.FindControl("lblLastName");
            Label lEmail = (Label)gr.FindControl("lblEmail");

            // Call your SQL server Data Access layer to save appropriate fields.

        }

    }
   

    protected void chkAll_CheckedChanged(object sender, EventArgs e)
    {
       
       
        foreach(GridViewRow gr in GridView1.Rows)
        {
            CheckBox cb = (CheckBox)gr.FindControl("chkItem");
            if(((CheckBox)sender).Checked)
             cb.Checked = true;
                else
             cb.Checked = false;
        }
       

       
    }


    protected void Button3_Click(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();

        foreach(GridViewRow gr in GridView1.Rows)
        {
            CheckBox cb = (CheckBox)gr.FindControl("chkItem");
            if(cb.Checked)
            {
                sb.Append(GridView1.DataKeys[gr.RowIndex]["Email"].ToString());
                sb.Append(",");
            }
        }
       
        MailMessage msg = new MailMessage("
mailto:your_hotmail_id@hotmail.com%22,sb.ToString());
        msg.Subject = txtSubject.Text;
        SmtpClient sc = new SmtpClient("smtp.live.com",25);
        sc.Credentials = new NetworkCredential("
mailto:your_hotmail_id@hotmail.com%22,%22Your_password");
        sc.EnableSsl = true;
        try
        {
        sc.Send(msg);
            Response.Write("<B>Email Has been sent successfully.</B>");
        }
        catch(Exception ex)
        {
            Response.Write(ex.Message);
        }
       

    }
}

Vb.Net code

Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

Imports System.Net.Mail
Imports System.Text
Imports System.Net


Public Partial Class _Default
 Inherits System.Web.UI.Page
 Protected Sub Page_Load(sender As Object, e As EventArgs)

  If Not Page.IsPostBack Then
   BindGrid()
  End If
 End Sub

 Private Sub BindGrid()
  GridView1.DataSource = GridDataProvider.GetData()

  GridView1.DataBind()

 End Sub
 Protected Sub Button2_Click(sender As Object, e As EventArgs)
  GridView1.ShowFooter = True
  BindGrid()
 End Sub
 Protected Sub Button1_Click(sender As Object, e As EventArgs)

 End Sub
 Protected Sub LinkButton1_Click(sender As Object, e As EventArgs)
  For Each gr As GridViewRow In GridView1.Rows
   Dim t As TextBox = DirectCast(gr.FindControl("txtFirstName"), TextBox)
   Dim tLastName As TextBox = DirectCast(gr.FindControl("txtLastName"), TextBox)
   Dim tEmail As TextBox = DirectCast(gr.FindControl("txtEmail"), TextBox)
   Dim lFirstName As Label = DirectCast(gr.FindControl("lblFirstName"), Label)
   Dim lLastName As Label = DirectCast(gr.FindControl("lblLastName"), Label)
   Dim lEmail As Label = DirectCast(gr.FindControl("lblEmail"), Label)

   If t.Visible Then
    t.Visible = False
    tLastName.Visible = False
    tEmail.Visible = False
    lFirstName.Visible = True
    lLastName.Visible = True
    lEmail.Visible = True
   Else
    t.Visible = True
    tLastName.Visible = True
    tEmail.Visible = True
    lFirstName.Visible = False
    lLastName.Visible = False

    lEmail.Visible = False


   End If
  Next
 End Sub
 Protected Sub LinkButton2_Click(sender As Object, e As EventArgs)
  For Each gr As GridViewRow In GridView1.Rows
   Dim t As TextBox = DirectCast(gr.FindControl("txtFirstName"), TextBox)
   Dim tLastName As TextBox = DirectCast(gr.FindControl("txtLastName"), TextBox)
   Dim tEmail As TextBox = DirectCast(gr.FindControl("txtEmail"), TextBox)
   Dim lFirstName As Label = DirectCast(gr.FindControl("lblFirstName"), Label)
   Dim lLastName As Label = DirectCast(gr.FindControl("lblLastName"), Label)

    ' Call your SQL server Data Access layer to save appropriate fields.

   Dim lEmail As Label = DirectCast(gr.FindControl("lblEmail"), Label)
  Next

 End Sub


 Protected Sub chkAll_CheckedChanged(sender As Object, e As EventArgs)


  For Each gr As GridViewRow In GridView1.Rows
   Dim cb As CheckBox = DirectCast(gr.FindControl("chkItem"), CheckBox)
   If (DirectCast(sender, CheckBox)).Checked Then
    cb.Checked = True
   Else
    cb.Checked = False
   End If
  Next

 End Sub


 Protected Sub Button3_Click(sender As Object, e As EventArgs)
  Dim sb As New StringBuilder()

  For Each gr As GridViewRow In GridView1.Rows
   Dim cb As CheckBox = DirectCast(gr.FindControl("chkItem"), CheckBox)
   If cb.Checked Then
    sb.Append(GridView1.DataKeys(gr.RowIndex)("Email").ToString())
    sb.Append(",")
   End If
  Next

  Dim msg As New MailMessage("your_hotmail_id@hotmail.com", sb.ToString())
  msg.Subject = txtSubject.Text
  Dim sc As New SmtpClient("smtp.live.com", 25)
  sc.Credentials = New NetworkCredential("
your_hotmail_id@hotmail.com", "Your_password")
  sc.EnableSsl = True
  Try
   sc.Send(msg)
   Response.Write("<B>Email Has been sent successfully.</B>")
  Catch ex As Exception
   Response.Write(ex.Message)
  End Try


 End Sub
End Class

'=======================================================
'Service provided by Telerik (
http://www.telerik.com/)
'Conversion powered by NRefactory.
'Built and maintained by Todd Anglin and Telerik
'=======================================================


For demo purpost I used hotmail smtp, you can use your own smtp to send customised emails.







Satalaj

Tags:

Temporary disposable emailYahoo email settingsJquery Gridview Dropdownlist Selected Change Enable Disable ControlsCheck Email AddressHow to get Jquery selected optionGridView RowGridView Editgridview add new rowC# send email using System.Net SMTP Clientasp.net Gridview checkboxJquery Dropdown Selected Value

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.