[英]How to refresh an ASP.NET GridView automatically after update
I need update the GridView
after send email to list of users registered in the table userTable
. 发送电子邮件到表
userTable
注册的用户列表后,我需要更新GridView
。
The GridView
is populated from 25 users at a time. GridView
填充了25个用户。
I send the email to 25 first users, updated the field SendEmail
for ID user
and now I need update the GridView
and show the next 25 users and so on. 我将电子邮件发送给25个第一用户,更新了
ID user
SendEmail
字段,现在我需要更新GridView
并显示接下来的25个用户,依此类推。
I have tried this solution but after send email and update field SendEmail
for ID user
I see always first 25 users. 我尝试了此解决方案,但在为
ID user
发送电子邮件和更新字段SendEmail
,我总是看到前25个用户。
What am I missing? 我想念什么?
What's wrong with this code? 此代码有什么问题?
Thank you in advance. 先感谢您。
My code c# asp net: 我的代码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();
This doesn't bind the grid , it binds the button . 这不绑定网格 ,而是绑定按钮 。 You need to re-bind the grid itself.
您需要重新绑定网格本身。 First abstract that logic into its own method:
首先将该逻辑抽象为自己的方法:
private void BindGrid()
{
// basically all the code from btnBind_Click
}
Then call it from your handlers. 然后从您的处理程序调用它。 For starters:
对于初学者:
protected void btnBind_Click(object sender, EventArgs e)
{
BindGrid();
}
Then also in your logic after sending the emails: 然后,在发送电子邮件后,按照您的逻辑:
cmd.ExecuteNonQuery();
Response.Write(sql1 + "<br /><br />");
BindGrid();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.