2014-10-02 61 views
0

后自动刷新ASP.NET的GridView我需要更新GridView发送电子邮件后,在表userTable注册用户的列表。如何更新

GridView从25个用户一次填充。

我发送电子邮件至25个用户第一,更新领域SendEmailID user,现在我需要更新GridView和显示下一个25个用户等等。

我已经试过这个解决方案,但后发送电子邮件和更新领域SendEmailID user我看到的总是先25个用户。

我错过了什么?

这段代码有什么问题?

预先感谢您。

我的代码C#ASP网:

protected void btnSend_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     SmtpClient smtpClient = new SmtpClient(); 
     System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage(); 

     lbltotalcount.Text = string.Empty; 

     foreach (GridViewRow grow in grvCustomers.Rows) 
     { 
      try 
      { 
       ID = grow.Cells[0].Text.Trim(); 
       name = grow.Cells[1].Text.Trim(); 
       email = grow.Cells[2].Text.Trim(); 

       //Send email; 

       using (OdbcConnection conn = 
            new OdbcConnection(ConfigurationManager.ConnectionStrings["cs"].ConnectionString)) 
       { 
        sql1 = " UPDATE userTable SET `SendEmail` = 1 WHERE ID = ?; "; 

        using (OdbcCommand cmd = new OdbcCommand(sql1, conn)) 
        { 
         try 
         { 
          conn.Open(); 
          cmd.Parameters.AddWithValue("param1", ID.ToString());         
          cmd.ExecuteNonQuery(); 
          Response.Write(sql1 + "<br /><br />"); 
          btnBind.DataBind(); 
         } 
         catch (Exception ex) 
         { 
          Response.Write("Send Email Failed." + ex.Message); 
         } 
         finally 
         { 
          conn.Close(); 
         } 
        } 
       } 
      } 
      catch (Exception ex) 
      { 
       Response.Write(ex.Message); 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     Response.Write(ex.Message); 
    } 
} 

protected void btnBind_Click(object sender, EventArgs e) 
{ 
    sql = " SELECT * from userTable WHERE `SendEmail` = 0 LIMIT 25; "; 

    using (OdbcConnection conn = 
         new OdbcConnection(ConfigurationManager.ConnectionStrings["cs"].ConnectionString)) 
    { 
     conn.Open(); 
     using (OdbcCommand cmd = new OdbcCommand(sql, conn)) 
     { 
      try 
      { 
       OdbcDataAdapter adp = new OdbcDataAdapter(cmd); 
       DataSet ds = new DataSet(); 
       adp.Fill(ds); 
       grvCustomers.DataSource = ds; 
       grvCustomers.DataBind(); 
       lbltotalcount.Text = grvCustomers.Rows.Count.ToString(); 
      } 
      catch (Exception ex) 
      { 
       Response.Write("Send Email Failed." + ex.Message); 
      } 
      finally 
      { 
       conn.Close(); 
      } 

      btnBind.Visible = false; 
     } 
    } 
} 

edit #1

protected void btnBind_Click(object sender, EventArgs e) 
{ 
    BindGrid(); 
} 



protected void btnSend_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     SmtpClient smtpClient = new SmtpClient(); 
     System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage(); 

     lbltotalcount.Text = string.Empty; 

     foreach (GridViewRow grow in grvCustomers.Rows) 
     { 
      try 
      { 
       ID = grow.Cells[0].Text.Trim(); 
       name = grow.Cells[1].Text.Trim(); 
       email = grow.Cells[2].Text.Trim(); 

       //Send email; 

       using (OdbcConnection conn = 
            new OdbcConnection(ConfigurationManager.ConnectionStrings["cs"].ConnectionString)) 
       { 
        sql1 = " UPDATE userTable SET `SendEmail` = 1 WHERE ID = ?; "; 

        using (OdbcCommand cmd = new OdbcCommand(sql1, conn)) 
        { 
         try 
         { 
          conn.Open(); 
          cmd.Parameters.AddWithValue("param1", ID.ToString());         
          cmd.ExecuteNonQuery(); 
          Response.Write(sql1 + "<br /><br />"); 

          BindGrid(); 

         } 
         catch (Exception ex) 
         { 
          Response.Write("Send Email Failed." + ex.Message); 
         } 
         finally 
         { 
          conn.Close(); 
         } 
        } 
       } 
      } 
      catch (Exception ex) 
      { 
       Response.Write(ex.Message); 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     Response.Write(ex.Message); 
    } 
} 

protected void BindGrid(); 
{ 
    sql = " SELECT * from userTable WHERE `SendEmail` = 0 LIMIT 25; "; 

    using (OdbcConnection conn = 
         new OdbcConnection(ConfigurationManager.ConnectionStrings["cs"].ConnectionString)) 
    { 
     conn.Open(); 
     using (OdbcCommand cmd = new OdbcCommand(sql, conn)) 
     { 
      try 
      { 
       OdbcDataAdapter adp = new OdbcDataAdapter(cmd); 
       DataSet ds = new DataSet(); 
       adp.Fill(ds); 
       grvCustomers.DataSource = ds; 
       grvCustomers.DataBind(); 
       lbltotalcount.Text = grvCustomers.Rows.Count.ToString(); 
      } 
      catch (Exception ex) 
      { 
       Response.Write("Send Email Failed." + ex.Message); 
      } 
      finally 
      { 
       conn.Close(); 
      } 

      btnBind.Visible = false; 
     } 
    } 
} 
+0

是您提交后重新绑定再次抓住你的数据吗? – Kritner 2014-10-02 18:53:02

+0

发送电子邮件后,您需要重新绑定GridView。 – 2014-10-02 18:53:13

+0

重新绑定网格对此无济于事。发送邮件后..获取下25条记录并将这25行再次绑定到网格。 – Rahul 2014-10-02 18:54:13

回答

1
btnBind.DataBind(); 

这不绑定电网,它结合了按钮。你需要重新绑定网格本身。首先将该逻辑抽象为自己的方法:

private void BindGrid() 
{ 
    // basically all the code from btnBind_Click 
} 

然后从您的处理程序中调用它。对于初学者:

protected void btnBind_Click(object sender, EventArgs e) 
{ 
    BindGrid(); 
} 

然后还你的逻辑发送邮件后:

cmd.ExecuteNonQuery(); 
Response.Write(sql1 + "<br /><br />"); 
BindGrid(); 
+0

谢谢,我已经编辑我的代码与您的建议,但现在我有这个错误'发送电子邮件失败。ConnectionString属性尚未初始化。请在第一篇文章中阅读编辑#1。 – 2014-10-02 19:05:39

1
protected void btnSend_Click(object sender, EventArgs e) 
{ 
after send mail and update table. then rebind data to gridview, 
For example: 
    try 
    { 
     //send email 

    //it will bind next 25 records 
    btnBind_Click(sender, e); 
    } 
}