control if email was already checked in gridview
I'll define the problem first. I have a gridview which has a column checkboxes and another column email adresses. If for example I select 3 rows out of 7 and these 3 rows have all the same email address, my code will send 3 emails to the address. Is it possible to get these 3 rows in 1 mail?
My current code looks like this:
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox ck = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBoxATH");
if (ck != null)
{
Label lblUsrE = (Label)GridView1.Rows[i].Cells[7].FindControl("LabelEmail");
开发者_StackOverflow中文版 string emadr = lblUsrE.Text.ToString();
if (ck.Checked == true)
{
MailMessage mail = new MailMessage();
mail.To.Add(emadr.ToString());
}}}
I was thinking of another loop around lblUsrE, but I could use some help here.
Regards Mati
List<string> lst = new List<string>();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox ck = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBoxATH");
if (ck != null)
{
Label lblUsrE = (Label)GridView1.Rows[i].Cells[7].FindControl("LabelEmail");
string emadr = lblUsrE.Text.ToString();
if (ck.Checked == true && !lst.Contains(emadr))
{
lst.Add(emadr);
MailMessage mail = new MailMessage();
mail.To.Add(emadr.ToString());
....
}
}
}
精彩评论