後自動刷新ASP.NET的GridView我需要更新GridView
發送電子郵件後,在表userTable
註冊用戶的列表。如何更新
GridView
從25個用戶一次填充。
我發送電子郵件至25個用戶第一,更新領域SendEmail
爲ID user
,現在我需要更新GridView
和顯示下一個25個用戶等等。
我已經試過這個解決方案,但後發送電子郵件和更新領域SendEmail
爲ID 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;
}
}
}
是您提交後重新綁定再次抓住你的數據嗎? – Kritner 2014-10-02 18:53:02
發送電子郵件後,您需要重新綁定GridView。 – 2014-10-02 18:53:13
重新綁定網格對此無濟於事。發送郵件後..獲取下25條記錄並將這25行再次綁定到網格。 – Rahul 2014-10-02 18:54:13