将 gridviewRow 添加到电子邮件控件上的 gridview

发布于 2024-11-02 07:18:21 字数 5181 浏览 1 评论 0原文

我正在尝试控制电子邮件地址是否已在列表中,如果是,我想从 gridview 添加一行到将发送到该电子邮件帐户的新 gridview。

我有一些代码来获取行并将它们发送到电子邮件地址。但问题是,即使检查了该地址的多行,它也仅向电子邮件地址发送 1 行。

请参阅此处我的代码:

protected void ButtonATH_Click(object sender, EventArgs e)
    {
        List<string> lst = new List<string>();
        for (int i = 0; i < GridViewBestelling.Rows.Count; i++)
        {
            CheckBox ck = (CheckBox)GridViewBestelling.Rows[i].Cells[0].FindControl("CheckBoxAfTeHalen");
            Label bestelID = (Label)GridViewBestelling.Rows[i].Cells[5].FindControl("LabelBestelID");
            Label lblUsrE = (Label)GridViewBestelling.Rows[i].Cells[7].FindControl("LabelEmailGeb");
            string emadr = lblUsrE.Text.ToString();
            string conn = "Data Source=pc-...";

            GridView grd = new GridView();

         if (ck.Checked)
            {
                System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection(conn);
                sqlConn.Open();
                System.Data.SqlClient.SqlCommand updateCommand = new System.Data.SqlClient.SqlCommand("UPDATE tblBestelling SET tBestAfTeHalen = '" + ck.Checked + "' WHERE tBestId= '" + bestelID.Text + "'", sqlConn);
                updateCommand.Parameters.AddWithValue("@bestID", bestelID.Text);
                updateCommand.ExecuteNonQuery();

                LabelSendGridBoven.Text = "<b>Hello: <br /><br /> ";
                LabelSendGridBoven.Visible = false;

                LabelHoeveelheid.Text = "<br /><br /> Amount" ;
                LabelHoeveelheid.Visible = false;

                LabelSendGridTussen.Text = "Regards";
                LabelSendGridTussen.Visible = false;

                LabelSendGridOnder.Text = "<br /><br />----------------------" ;
                LabelSendGridOnder.Visible = false;

                DataTable dt = new DataTable();
                DataRow dr;

                dt.Columns.Add(new DataColumn("B_ID"));
                dt.Columns.Add(new DataColumn("P"));
                dt.Columns.Add(new DataColumn("M"));
                dt.Columns.Add(new DataColumn("H"));

                dr = dt.NewRow();
                dr["B_ID"] = ((Label)GridViewBestelling.Rows[i].Cells[5].FindControl("LabelBestelID")).Text;
                dr["P"] = ((Label)GridViewBestelling.Rows[i].Cells[8].FindControl("LabelProduct_naam")).Text;
                dr["M"] = ((Label)GridViewBestelling.Rows[i].Cells[9].FindControl("LabelManufact_Nr")).Text;
                dr["H"] = ((Label)GridViewBestelling.Rows[i].Cells[3].FindControl("LabelHoeveelheid")).Text;
                dt.Rows.Add(dr);

                grd.DataSource = dt;
                grd.DataBind();

                StringBuilder sb = new StringBuilder();
                StringWriter sw = new StringWriter(sb);
                HtmlTextWriter htw = new HtmlTextWriter(sw);
                grd.RenderControl(htw);

              if ( !lst.Contains(emadr))
              {
                  lst.Add(emadr);

                  System.Data.SqlClient.SqlConnection sqlConn2 = new System.Data.SqlClient.SqlConnection(conn);
                  sqlConn2.Open();
                  System.Data.SqlClient.SqlCommand updateCommand2 = new System.Data.SqlClient.SqlCommand("UPDATE tblBestelling SET Status=@statusChange WHERE tBestId= '" + bestelID.Text + "'", sqlConn2);
                  updateCommand2.Parameters.AddWithValue("@statusChange", statusChange.ToString());
                  updateCommand2.Parameters.AddWithValue("@bestID", bestelID.Text);
                  updateCommand2.ExecuteNonQuery();
                  sqlConn2.Close();

                    try
                    {
                        MailMessage mail = new MailMessage();
                        mail.To.Add(emadr.ToString());
                        mail.Bcc.Add("SomeoneBcc");
                        mail.From = new MailAddress("FromWho");
                        mail.Subject = "SomeSubject";
                        string Body = LabelSendGridBoven.Text + sb.ToString() + LabelHoeveelheid.Text + LabelSendGridTussen.Text + "<br /><img alt=\"\" hspace=0 src=\"cid:imageId\" align=baseline border=0 >" + LabelSendGridOnder.Text + "<BR>";

                        AlternateView htmlView = AlternateView.CreateAlternateViewFromString(Body, null, "text/html");
                        LinkedResource imagelink = new LinkedResource(Server.MapPath(".") + @"\logo\Logo.jpg");
                        imagelink.ContentId = "imageId";
                        imagelink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
                        htmlView.LinkedResources.Add(imagelink);
                        mail.AlternateViews.Add(htmlView);
                        SmtpClient smtp = new SmtpClient("..server..");
                        smtp.Send(mail);
                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex.Message);
                    }
                }
            }
        }
    }

这会向每个电子邮件帐户发送一封电子邮件,并检查所有行,但我只想要与电子邮件地址相对应的行。

请问有人有想法吗?

I am trying to control whether or not an email address is already in a list, and if so i'd like to add a row from a gridview to the new gridview that will be send to that email account.

I have some code to get the rows and send them to the email address. But the problem is that it only sends 1 row to an email address even though multiple rows for that address were checked.

See here my code:

protected void ButtonATH_Click(object sender, EventArgs e)
    {
        List<string> lst = new List<string>();
        for (int i = 0; i < GridViewBestelling.Rows.Count; i++)
        {
            CheckBox ck = (CheckBox)GridViewBestelling.Rows[i].Cells[0].FindControl("CheckBoxAfTeHalen");
            Label bestelID = (Label)GridViewBestelling.Rows[i].Cells[5].FindControl("LabelBestelID");
            Label lblUsrE = (Label)GridViewBestelling.Rows[i].Cells[7].FindControl("LabelEmailGeb");
            string emadr = lblUsrE.Text.ToString();
            string conn = "Data Source=pc-...";

            GridView grd = new GridView();

         if (ck.Checked)
            {
                System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection(conn);
                sqlConn.Open();
                System.Data.SqlClient.SqlCommand updateCommand = new System.Data.SqlClient.SqlCommand("UPDATE tblBestelling SET tBestAfTeHalen = '" + ck.Checked + "' WHERE tBestId= '" + bestelID.Text + "'", sqlConn);
                updateCommand.Parameters.AddWithValue("@bestID", bestelID.Text);
                updateCommand.ExecuteNonQuery();

                LabelSendGridBoven.Text = "<b>Hello: <br /><br /> ";
                LabelSendGridBoven.Visible = false;

                LabelHoeveelheid.Text = "<br /><br /> Amount" ;
                LabelHoeveelheid.Visible = false;

                LabelSendGridTussen.Text = "Regards";
                LabelSendGridTussen.Visible = false;

                LabelSendGridOnder.Text = "<br /><br />----------------------" ;
                LabelSendGridOnder.Visible = false;

                DataTable dt = new DataTable();
                DataRow dr;

                dt.Columns.Add(new DataColumn("B_ID"));
                dt.Columns.Add(new DataColumn("P"));
                dt.Columns.Add(new DataColumn("M"));
                dt.Columns.Add(new DataColumn("H"));

                dr = dt.NewRow();
                dr["B_ID"] = ((Label)GridViewBestelling.Rows[i].Cells[5].FindControl("LabelBestelID")).Text;
                dr["P"] = ((Label)GridViewBestelling.Rows[i].Cells[8].FindControl("LabelProduct_naam")).Text;
                dr["M"] = ((Label)GridViewBestelling.Rows[i].Cells[9].FindControl("LabelManufact_Nr")).Text;
                dr["H"] = ((Label)GridViewBestelling.Rows[i].Cells[3].FindControl("LabelHoeveelheid")).Text;
                dt.Rows.Add(dr);

                grd.DataSource = dt;
                grd.DataBind();

                StringBuilder sb = new StringBuilder();
                StringWriter sw = new StringWriter(sb);
                HtmlTextWriter htw = new HtmlTextWriter(sw);
                grd.RenderControl(htw);

              if ( !lst.Contains(emadr))
              {
                  lst.Add(emadr);

                  System.Data.SqlClient.SqlConnection sqlConn2 = new System.Data.SqlClient.SqlConnection(conn);
                  sqlConn2.Open();
                  System.Data.SqlClient.SqlCommand updateCommand2 = new System.Data.SqlClient.SqlCommand("UPDATE tblBestelling SET Status=@statusChange WHERE tBestId= '" + bestelID.Text + "'", sqlConn2);
                  updateCommand2.Parameters.AddWithValue("@statusChange", statusChange.ToString());
                  updateCommand2.Parameters.AddWithValue("@bestID", bestelID.Text);
                  updateCommand2.ExecuteNonQuery();
                  sqlConn2.Close();

                    try
                    {
                        MailMessage mail = new MailMessage();
                        mail.To.Add(emadr.ToString());
                        mail.Bcc.Add("SomeoneBcc");
                        mail.From = new MailAddress("FromWho");
                        mail.Subject = "SomeSubject";
                        string Body = LabelSendGridBoven.Text + sb.ToString() + LabelHoeveelheid.Text + LabelSendGridTussen.Text + "<br /><img alt=\"\" hspace=0 src=\"cid:imageId\" align=baseline border=0 >" + LabelSendGridOnder.Text + "<BR>";

                        AlternateView htmlView = AlternateView.CreateAlternateViewFromString(Body, null, "text/html");
                        LinkedResource imagelink = new LinkedResource(Server.MapPath(".") + @"\logo\Logo.jpg");
                        imagelink.ContentId = "imageId";
                        imagelink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
                        htmlView.LinkedResources.Add(imagelink);
                        mail.AlternateViews.Add(htmlView);
                        SmtpClient smtp = new SmtpClient("..server..");
                        smtp.Send(mail);
                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex.Message);
                    }
                }
            }
        }
    }

This results in 1 email to each email account which was checked with all rows, but I would like only the rows corresponding with the email address.

Anyone an idea please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃兔兔 2024-11-09 07:18:21
lst.Contains(emadr).ToString() == emadr.ToString()

那么 lst.Contains() 返回一个布尔值,您可以将其转换为字符串,因此除非您的电子邮件看起来像 truefalse 它不会工作

为什么不直接使用:

if(lst.Contains(emadr))
{...}
lst.Contains(emadr).ToString() == emadr.ToString()

Well lst.Contains() returns a bool, which you convert to a string, so unless your email looks like true or false it's not going to work

Why not just use:

if(lst.Contains(emadr))
{...}
泅渡 2024-11-09 07:18:21

我认为你在这里做错了

if (lst.Contains(emadr).ToString() == emadr.ToString())
  {...}

lst.Contains(emadr).ToString() 是 true 或 false,并且永远不会匹配成员中的电子邮件地址 emadr

来自您应该使用提到的评论

if (lst.Contains(emadr))
{
//your logic
}

I think you are doing something wrong here

if (lst.Contains(emadr).ToString() == emadr.ToString())
  {...}

lst.Contains(emadr).ToString() would be true or false and will never match the email address in the memeber emadr

From the comments as mentioned you should use

if (lst.Contains(emadr))
{
//your logic
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文