简体   繁体   中英

How to make a button refill a GridView each time it's clicked

I have a dropdown menu in Visual Studio and when a selection is made, a user clicks a button. Then the button fills a GridView.

It works for one time, but if I select something a second time and click the button, nothing happens. How do I make it refresh or do the action again?

Button Click in C#:

 protected void ButtonChangeEvent_Click(object sender, EventArgs e)
    {
        string constr = ConfigurationManager.ConnectionStrings["Events2"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("spRegistrantsGridView"))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Action", "SELECT");
                cmd.Parameters.Add("@EventId", SqlDbType.Int).Value = DropDownListEvent.SelectedValue;
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        GridView1.DataSource = dt;
                        GridView1.DataBind();
                    }
                }
            }
        }
    }

Check the value of DropDownListEvent.SelectedValue when you click button for 2nd time. I suspect it takes the 1st value from ddl always. This happens if you have bound ddl in Page_Load event handler without putting !IsPostBack condition check.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM