Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4.5 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4.5: in C# and VB by Imar Spaanjaars; ISBN: 978-1-118-31180-6
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4.5 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 25th, 2013, 10:52 PM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2013
Location: Malaysia
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via Yahoo to et_leen
Default sending value from checkbox to email

I want to send an email with the value from the checkbox (retrieve from database).
I'm using 'for' to retrieve the selected item from checkbox. But the email was sending based on how many selection I made. What is the best way to retrieve the checkbox values and attached the values in the email?
Below is my code:
sending_checkbox_value.aspx

Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="sending_checkbox_value.aspx.cs" Inherits="sending_checkbox_value" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>

     <script>
         function SelectAll(id) {
             var frm = document.forms[0];
             for (i = 0; i < frm.elements.length; i++) {
                 if (frm.elements[i].type == "checkbox") {
                     frm.elements[i].checked = document.getElementById(id).checked;
                 }
             }
         }
</script>
<body>
    <form id="form1" runat="server">
    <div>
        This form is to test to sending an email<br />
        <br />
        Email address:
        <asp:TextBox ID="Email" runat="server"></asp:TextBox>
        <asp:SqlDataSource ID="reg_type_ds" runat="server" ConnectionString="<%$ ConnectionStrings:MyDbConn %>" SelectCommand="SELECT [ID], [UserType]FROM [UserType]"></asp:SqlDataSource>
        <asp:GridView ID="RegistrationType" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="reg_type_ds" EnableModelValidation="True"  onrowdatabound="GridView1_RowDataBound">
            <Columns>
                <asp:TemplateField>
        <ItemTemplate>         
            <asp:CheckBox ID="CheckBox1" runat="server" />
        </ItemTemplate>
        <%--   if we no need Check box in header then hide it below Header Template and also comment server side row data bound code--%>
           <HeaderTemplate>                     
                <asp:CheckBox ID="SelectAll" runat="server" OnCheckedChanged="SelectAll_CheckedChanged"/>
            </HeaderTemplate>
            <EditItemTemplate>
                <asp:CheckBox ID="Chkbx" runat="server" />
            </EditItemTemplate>
        </asp:TemplateField>
                <asp:BoundField DataField="UserType" HeaderText="Registration Type" SortExpression="UserType" />
            </Columns>
        </asp:GridView>
        <br />
        <asp:Button ID="submit_btn" runat="server"  Text="Submit" OnClick="submit_btn_Click" />
    </div>
    </form>
</body>
</html>
the code behind: sending_checkbox_value.aspx.cs

Code:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

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

    protected void submit_btn_Click(object sender, EventArgs e)
    {
        for (int i = 0; i <= RegistrationType.Rows.Count - 1; i++)
        {
            string toemail = RegistrationType.Rows[i].Cells[1].Text;
            GridViewRow row = RegistrationType.Rows[i];
            CheckBox Ckbox = (CheckBox)row.FindControl("CheckBox1");
            if (Ckbox.Checked == true)
            {
                sendMail(toemail);
            }
            Response.Write(toemail);
        }
    }
         
        void sendMail(String result)
        {
       
        //register the email and password for network credential
           string UserName = "abcy@business.com";
           string Password = "abcdefgh";

           using (MailMessage message = new MailMessage()){
            //set the sender email
            message.From = new MailAddress(Email.Text.ToString());
            //set the receiver email
            message.To.Add(new MailAddress("admin@business.com"));
            // set the message subject
            message.Subject = "New Email from " + Email.Text.ToString();
            //to enable the email content to have text formating
            message.IsBodyHtml = true;
            //set the email content/body
            message.Body = "<strong><u>Sending testing email</u></strong><br><br>Email from : " + Email.Text.ToString() + "<br> Registration type : " + result;
            //set the smtp resource
            SmtpClient client = new SmtpClient("mail.business.com", 26);
            //set the network credential
            System.Net.NetworkCredential networkcred = new System.Net.NetworkCredential();
            networkcred.UserName = UserName;
            networkcred.Password = Password;
            client.UseDefaultCredentials = false;
            client.Credentials = networkcred;
            client.Send(message);
            Response.Write("E-mail sent! <br>");
           }
        }
    
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if ((e.Row.RowType == DataControlRowType.Header))
        {
            ((CheckBox)e.Row.FindControl("SelectAll")).Attributes.Add("onclick", "javascript:SelectAll('" + ((CheckBox)e.Row.FindControl("SelectAll")).ClientID + "')");
        }
    }

    protected void SelectAll_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chk;
        foreach (GridViewRow rowItem in RegistrationType.Rows)
        {
            chk = (CheckBox)(rowItem.Cells[0].FindControl("CheckBox1"));
            chk.Checked = ((CheckBox)sender).Checked;
        }  
    } 
}
 
Old March 26th, 2013, 11:40 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Since this is not related to my book directly, may I suggest you post this in a more general ASP.NET category from this list: http://p2p.wrox.com/asp-net-4-5-709/

If you do post there, please add a more detailed problem description as currently it's hard to understand the problem.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!




Similar Threads
Thread Thread Starter Forum Replies Last Post
Sending EMAIL charleshua BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 16 September 3rd, 2008 12:27 AM
Sending email ironchef Pro JSP 3 February 17th, 2008 11:38 PM
Need Help in sending email khalidj06 Forum and Wrox.com Feedback 6 April 14th, 2006 08:16 AM
email sending msrnivas .NET Web Services 6 June 26th, 2004 06:59 AM
Checkbox input won't email scottiegirl PHP How-To 3 August 6th, 2003 07:40 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.