在gridview中激活和停用用户

时间:2016-06-09 07:09:25

标签: c# asp.net gridview phpmyadmin

enter image description here

enter image description here

我的“激活/取消激活”按钮出现问题。当我按下Deactivate按钮时,它将切换到“Inactive”状态并更新我的数据库。但当我按下激活按钮时没有任何反应。我尝试了很多东西,但我发现我的代码出了什么问题。希望有人能听到我的声音。谢谢!

    protected void BindGridView()
    {
        DataTable dt = new DataTable();
        MySqlDataAdapter da = new MySqlDataAdapter("Select id,username,user_type,first_name,last_name,email,address,contact_number,status FROM user_tbl", con);

        con.Open();
        da.Fill(dt);
        con.Close();

        if (dt.Rows.Count > 0)
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }


    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        // id of edit row
        string id = GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
        string status = GridView1.Rows[e.NewSelectedIndex].Cells[7].Text;
        int newstat = 0;
        if (status == "Active")
        {
            status = "Inactive";
            newstat = 0;
        }
        if (status == "Inactive")
        {
            status = "Active";
            newstat = 1;
        }
        // update record
        MySqlCommand cmd = new MySqlCommand("update user_tbl set status='" + newstat + "'where ID=" + id, con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();

        BindGridView();
    }

1 个答案:

答案 0 :(得分:0)

如果点击两次相同按钮,我认为SelectedIndexChanging不会第二次触发。尝试将您的代码从SelectedIndexChanging放入OnClick="EventHandler"

<asp:Button OnClick="myButtonClick" />

void myButtonClick(Object sender, EventArgs e)
{
      ...
}
相关问题