I'm trying to send mails to selected users by selecting check box in grid view...
while debugging its not taking checkbox.checked=true....
kindly share your view and point out the mistake in codings...
source :
<%@ Page Language="C#" Debug="true" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
subject | |
| message |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Net.Mail;
using System.Net;
public partial class _Default : System.Web.UI.Page
{
Class1 obj = new Class1();
public string host, frommail, password;
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = obj.display();
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void btnsubmit_Click(object sender, EventArgs e)
{
MailMessage s = new MailMessage();
s.From = new MailAddress("[email protected]");
s.Subject = txtsubject.Text;
s.Body = txtmessage.Text;
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chk = (CheckBox)row.FindControl("CheckBox1");
{
if (chk.Checked==true)
{
string email = row.Cells[2].Text;
s.To.Add(email);
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential p = new NetworkCredential();
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "******");
smtp.EnableSsl = true;
smtp.Send(s);
}
}
}
}
}
}
class file...........................................................................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Net.Mail;
using System.Net;
public class Class1
{
string s1 = ConfigurationManager.ConnectionStrings["pri"].ToString();
SqlCommand cmd = new SqlCommand();
public DataSet display()
{
using (SqlConnection con = new SqlConnection(s1))
{
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_display";
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}

Nilesh Dhande9 reputationPosted Mar 17, 2014, 11:02 PM EDT
Your all code is correct just small change required
1. add not isposback on page load
if (!IsPostBack)
{
}
2. please add Autoposback true in checkbox
please do this
Brijendra GautamRank #1,900 · 778 reputationPosted Mar 18, 2014, 12:55 AM EDT
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet ds = obj.display();
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
This will solve your problem, no need to put autopostback="true" in checkbox.
Problem was when you clicked the button Page_load event occurs and then your gridview again bind with the dataset and causing checkboxes to initialize to default value and your checked option will get lost.