![](/img/trans.png)
[英]how can i update Gridview automatically when a record inserted of updated in asp.net without refresh page?
[英]How to refresh an ASP.NET GridView automatically after update
发送电子邮件到表userTable
注册的用户列表后,我需要更新GridView
。
GridView
填充了25个用户。
我将电子邮件发送给25个第一用户,更新了ID user
SendEmail
字段,现在我需要更新GridView
并显示接下来的25个用户,依此类推。
我尝试了此解决方案,但在为ID user
发送电子邮件和更新字段SendEmail
,我总是看到前25个用户。
我想念什么?
此代码有什么问题?
先感谢您。
我的代码C#ASP Net:
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;
}
}
}
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);
}
}
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.