2 Replies - 7590 Views - Last Post: 27 March 2013 - 08:37 PM Rate Topic: -----

#1 et_leen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-March 13

send email with checkbox values

Posted 25 March 2013 - 07:46 PM

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:

<%@ 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">

         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;
    <form id="form1" runat="server">
        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">
            <asp:CheckBox ID="CheckBox1" runat="server" />
        <%--   if we no need Check box in header then hide it below Header Template and also comment server side row data bound code--%>
                <asp:CheckBox ID="SelectAll" runat="server" OnCheckedChanged="SelectAll_CheckedChanged"/>
                <asp:CheckBox ID="Chkbx" runat="server" />
                <asp:BoundField DataField="UserType" HeaderText="Registration Type" SortExpression="UserType" />
        <br />
        <asp:Button ID="submit_btn" runat="server"  Text="Submit" onclick="submit_btn_Click" />

the code behind: sending_checkbox_value.aspx.cs

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)
        void sendMail(String result)
        //register the email and password for network credential
           string UserName = "[email protected]";
           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("[email protected]"));
            // 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;
            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;

Is This A Good Question/Topic? 0
  • +

Replies To: send email with checkbox values

#2 h4nnib4l   User is offline

  • The Noid
  • member icon

Reputation: 1330
  • View blog
  • Posts: 1,888
  • Joined: 24-August 11

Re: send email with checkbox values

Posted 26 March 2013 - 11:46 AM

You keep saying the "value from the checkbox", but are you really meaning the value from the GridView cell next to the checkbox? I'm going to assume so, because it is pretty easy to check the state of a checkbox (you have an example in your code).

So the way I understand this, what you're trying to do is iterate through each row of your GridView and, if the checkbox for that row is checked, send an email using data from that row? That's what it looks like your code is trying to do. If that is incorrect, please disregard my advice and square me away.

DISCLAIMER: It is bad design to perform all of these functions in an event handler (you should create methods to complete different tasks, and call methods from within the event handler), and you should have a function that maps variables, properties, etc., to fields from the form, but to avoid too much scope creep on my answer, I'll use what you have. :)/>

I think foreach (GridViewRow row in RegistrationType.Rows){} will serve you better than that for loop. It's cleaner (to me at least) and avoids the need for the type cast (since you explicitly declare the type in the loop initialization statement. So the item in row.Cells[0] is your checkbox, correct? So examine the checkbox in row.Cells[0] and then you can use your if statement, passing the contents from Cells[1] to your sendEmail method. Also, I feel like I saw a "select all" checkbox that checks every checkbox in the GridView. So that foreach needs to be preceded by a check for that checkbox, and if it is checked, execute a separate set of behavior that doesn't force a check on your iteration through each individual row.

All that being said, after a cursory glance, your code seems fine: what is it doing that you want to change, or what is it not doing that it needs to do? Please be as specific as possible. You seem to be making an attempt at doing what you asked: what is wrong with that attempt?

This post has been edited by h4nnib4l: 28 March 2013 - 05:03 AM

Was This Post Helpful? 1
  • +
  • -

#3 et_leen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-March 13

Re: send email with checkbox values

Posted 27 March 2013 - 08:37 PM

Hi h4nnib4l,

Thank you for your reply.
Your comment help me a lot. :bananaman:
I have change the
for (int i = 0; i <= RegistrationType.Rows.Count - 1; i++)

foreach (GridViewRow row in RegistrationType.Rows){} 

I also made some changes in the code and seems my code was successfully running.
Thank you again for your help. I really appreciate that..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1