简体   繁体   English

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

[英]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.

相关问题 在没有刷新页的情况下在asp.net中插入更新的记录时,如何自动更新Gridview? - how can i update Gridview automatically when a record inserted of updated in asp.net without refresh page? 如何通过ASP.NET中的计时器自动刷新Gridview? - How to refresh a Gridview automatically by a Timer in asp.net? 如何在asp.net中手动刷新而无需更新gridview中的数据? - How to update data in gridview without manual refresh in asp.net? 在asp.net C#中插入后如何刷新GridView - How to refresh gridview after insertion in asp.net c# asp.net - gridview 更新刷新页面并重新加载数据 - asp.net - gridview update refresh page and reload data 如何在gridview(asp.net)进入更新模式之前刷新整个页面? - How to refresh whole page before going into update mode in gridview(asp.net)? 如何防止在Asp.Net Gridview中的页面更改上刷新页面 - How to prevent page refresh on Page Changing in Asp.Net Gridview 在asp.net中使用sqldatasource时如何刷新GridView - How to refresh gridview when I use sqldatasource in asp.net 如何在不使用c#(asp.net)运行项目的情况下30分钟后自动刷新页面 - how to refresh page after 30 minutes automatically without run the project using c#(asp.net) 使用数据库自​​动创建ASP.NET GridView - Create ASP.NET GridView automatically with database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM