Subscribe to Subscribers
Talk to Webmaster Tony John

Online Members

Migbar
More...


Resources » Code Snippets » ASP.NET GridView

How to send email for selected record in Grid view using check box column?


Posted Date:     Category: ASP.NET GridView    
Author: Member Level: Diamond    Points: 10


In this article I have explained about how to send e-mail to the selected user in the grid view check box. In the example application I used check box column for user select option after user click “send email” button I check which user option is clicked I send mail to that user mail id only.



 


How to create check box column in grid view?
You can create check box column in the grid view using template field. Put the check box control in to the template field item template field.

How to select all check box in the grid view when user click header column of check box control?
Use javascript function to check/uncheck all check boxes in the grid view when user click header check box like below code
Client side

<script language="javascript" type="text/javascript">
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>

How to send email based on Grid View selected checkbox?
After user click send email button I have validate user select atleast one check box and also validate user enter text in the messeage box or not using below code

protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text == "")
{
Page.RegisterStartupScript("Alert Message", "<script language='javascript'>alert('Enter Message in Text Box');</script>");
return;
}

//Checkther whether atleast one check box is selected or not
for(int i=0;i<=GridView1.Rows.Count-1;i++)
{
GridViewRow row = GridView1.Rows[i];
CheckBox Ckbox = (CheckBox)row.FindControl("CheckBox1");
if (Ckbox.Checked == true)
{
k++;
}
}

if (k == 0)
{
Page.RegisterStartupScript("Alert Message", "<script language='javascript'>alert('Select atleast one Check box further to proceed');</script>");
return;
}

for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
string toemail = GridView1.Rows[i].Cells[3].Text;
GridViewRow row = GridView1.Rows[i];
CheckBox Ckbox = (CheckBox)row.FindControl("CheckBox1");
if (Ckbox.Checked == true)
{
sendMail(toemail);
}
}
TextBox1.Text = "";
}

void sendMail(String toemail)
{
try
{
//Below I mention From id(Gmail), To mail ID, Subject of the mail, Mail Message
MailMessage msg = new MailMessage("fromid@gmail.com", toemail , "Testing Mail", TextBox1.Text);
SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);
//Change your gmail user id and password for send email
NetworkCredential NetCrd = new NetworkCredential("fromid@gmail.com", "fromidpassword");
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = NetCrd;
mailClient.EnableSsl = true;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.Send(msg);
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}

Source Code Detail:
Here with I have attached source code to for grid view check box column with email option. Download it and try to send email for various users at the same time single button click.
Front End : ASP.NET
Code Behind : C#

Conclusion:
I hope this Article is help to you send email for multiple user.

Attachments
  • GridView_ChkBox_MailSend (42891-291029-GridView_MailSent_chkoption.rar)





  • Did you like this resource? Share it with your friends and show your love!


    Responses to "How to send email for selected record in Grid view using check box column?"

    No responses found. Be the first to respond...

    Feedbacks      

    Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: How to change grid view row back color based on condition using ASP.NET
    Previous Resource: Export Grid Data To Excel
    Return to Resources
    Post New Resource
    Category: ASP.NET GridView


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    How to use send email to multiple user in ASP.NET?  .  How to show checkbox column in grid view  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.