Results 1 to 1 of 1

Thread: Send multiple emails from a GridView, selected by checkboxes

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,173

    Post Send multiple emails from a GridView, selected by checkboxes

    [Javascript version, codebehind version to be submitted by szlamany]

    This code sample is useful if you want to open the user's email client (Outlook, Outlook Express, Windows Live Mail, etc.) from your ASP.NET page and if your gridview contains checkboxes to select to whom the email will go to.



    In my ASPX, I first make the javascript.

    Code:
     <script type="text/javascript">
      
    
            Array.prototype.remove=function(str)
            {
              for(i=0;i<this.length;i++)
              {
                if(this[i]==str) 
                {
                    this.splice(i, 1);
                }
              }
            }
    
            var EmailAddresses = new Array();
    
            function AddRemoveEmailAddress(chk, emailaddress)
            {
                if(chk.checked == true)
                {
                    EmailAddresses.push(emailaddress);
                }
                else
                {
                    EmailAddresses.remove(emailaddress);
                }
                
            }
            
            
            function SendEmails()
            {
            
                var emailString = "";
                
                for(i=0;i<EmailAddresses.length;i++)
                {
                    emailString += EmailAddresses[i] + ";";
                }
                
               document.location.href='mailto:' + emailString;
               
            }
    
         </script>
    Then, setup the gridview and button.

    Code:
     <div>
        <asp:GridView ID="Students" Runat="server" OnRowDataBound="Students_RowDataBound">
        <Columns>
        <asp:TemplateField>
        <ItemTemplate>
        <asp:CheckBox ID="chkSendMail" runat="server" />
        </ItemTemplate>
        </asp:TemplateField>
        </Columns>
        </asp:GridView>
        </div>
        <div>
        <input type="button" onclick="javascript:SendEmails();" value="Send" id="EmailButton" />
        </div>

    Then in the codebehind, I first setup a dummy datatable.

    vb Code:
    1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    2.  
    3.  
    4.         Dim dt As New Data.DataTable()
    5.         dt.Columns.Add("col1")
    6.         dt.Columns.Add("col2")
    7.         dt.Columns.Add("col3")
    8.  
    9.         Dim dr As Data.DataRow = dt.NewRow()
    10.         dr.Item("col1") = "111"
    11.         dr.Item("col2") = "222"
    12.         dr.Item("col3") = "gremlins@york.net"
    13.  
    14.         Dim dr2 As Data.DataRow = dt.NewRow()
    15.         dr2.Item("col1") = "333"
    16.         dr2.Item("col2") = "444"
    17.         dr2("col3") = "szlamany@philosophy.org"
    18.  
    19.  
    20.         Dim dr3 As Data.DataRow = dt.NewRow()
    21.         dr3.Item("col1") = "555"
    22.         dr3.Item("col2") = "666"
    23.         dr3.Item("col3") = "hamburgers@microsoft.com"
    24.  
    25.  
    26.         dt.Rows.Add(dr)
    27.         dt.Rows.Add(dr2)
    28.         dt.Rows.Add(dr3)
    29.  
    30.  
    31.  
    32.         Students.DataSource = dt
    33.         Students.DataBind()
    34.  
    35.  
    36.  
    37.     End Sub



    And finally the main bit, the RowDataBound event wherein I give each checkbox an onClick javascript method to call which adds or removes the email address from the global javascript array defined earlier.


    vb Code:
    1. Protected Sub Students_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Students.RowDataBound
    2.         If e.Row.RowType = DataControlRowType.DataRow Then
    3.             Dim chk As New CheckBox
    4.             chk = e.Row.FindControl("chkSendMail")
    5.             chk.Attributes.Add("onClick", "AddRemoveEmailAddress(this, '" & CType(e.Row.DataItem, Data.DataRowView).Item("col3").ToString() & "');")
    6.  
    7.  
    8.         End If
    9.     End Sub


    Ta-daaaa.

    Keywords:
    email
    gridview
    checkboxes
    client side
    Attached Images Attached Images  

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width