如何在按钮点击事件上刷新(清除GV数据)gridview? C#ASP.NET [英] How to refresh(clear the GV data) gridview on button click event? C# ASP.NET

查看:193
本文介绍了如何在按钮点击事件上刷新(清除GV数据)gridview? C#ASP.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在按钮点击输入时将数据绑定到gridview,如果记录存在,GV将显示数据,如果没有输入数据的记录则会显示一些消息,

当IF条件当IF条件不为真时,是真正的数据加载和显示,那么msg将显示,但旧的GV数据仍将显示在页面上,如何清除该数据?我希望在IF条件不成立时只显示消息。



I am binding the data to gridview on button click with input,if records exist GV will display data, if there is no records with input data then it will display some msg,
When IF condition is true data loads and diplays when IF condition is not true then msg will display but old GV data will still display on page, How to clear that data ? i want display only msg when IF condition is not true.

protected void btnGV1_Click(object sender, EventArgs e)
{
    string personid = TBpersonid.Text;
    String connstring = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
    SqlConnection con = new SqlConnection(connstring);
    SqlCommand cmd = new SqlCommand("select * from[Transactions_log] where person_id=@personid ", con);
    cmd.Parameters.AddWithValue("@personid",personid);
    con.Open();
    SqlDataReader Er = cmd.ExecuteReader();
    if (Er.HasRows==true)
    {
        GV1.DataSource = Er;
        GV1.DataBind();
    }
    else
    {
        Response.Write("No records found for the user: "+personid+"");
    }
}





我尝试了什么:



如果(!IsPostBack)有效吗?如果是,如何实现它?



What I have tried:

if (!IsPostBack) works ? if yes how to implement it?

推荐答案

GridView 只会显示是否有关联的数据。您是否在 Page_Load 事件中绑定了 GridView ?如果你这样做,那么请确保你的代码包装好! IsPosBack 块:



GridView will only display if there's data associated on it. Have you binded your GridView on Page_Load event? If you did, then make sure that you wrap your code within !IsPosBack block:

protected void Page_Load(object sender, EventArgs e)
{
     if(!IsPostBack){
           //bind your gridview here
     }
}





如果你想强制你的 GridView 清除数据并隐藏它,那么你可以将数据源设置为这样的:





If you want to force your GridView to clear the data and hide it, then you can set the datasource to nothing like this:

GV1.DataSource = null;
Gv1.DataBind();


这篇关于如何在按钮点击事件上刷新(清除GV数据)gridview? C#ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆