如何在更新后自动刷新ASP.NET GridView

时间:2014-10-02 18:49:10

标签: c# asp.net gridview

我需要在将电子邮件发送到表 GridView 中注册的用户列表后更新 userTable

GridView 一次填充25个用户。

我向25位第一批用户发送了电子邮件,为 SendEmail 更新了字段 ID user ,现在我需要更新 { {1}} 并显示接下来的25个用户,依此类推。

我尝试过此解决方案,但在发送电子邮件和更新字段 GridView SendEmail 后,我总是会看到前25位用户。

我错过了什么?

此代码有什么问题?

提前谢谢。

我的代码c#asp net:

ID user

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

2 个答案:

答案 0 :(得分: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);
    }
 }

答案 1 :(得分: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();