dhoni kholi

dhoni kholi

  • NA
  • 198
  • 42.8k

How to Hide Button In Gridview After Click

Jan 25 2020 8:43 AM
This is my Code
 
How to Hide De Active Buttons 
  1. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"   
  2.                DataKeyNames="id" >  
  3.         <Columns>  
  4.          <asp:BoundField DataField="id" HeaderText="id" />  
  5.             <asp:BoundField DataField="username" HeaderText="Name" />  
  6.             <asp:TemplateField>  
  7.                 <ItemTemplate>  
  8.                 <div class="btn btn-warning fa fa-check">  
  9.                     <asp:Button ID="btnChangeStatus" runat="server" OnClick="ChangeStatus" Text='<%# Eval("Status").ToString() == "True" ? "Active" : "De Activate"  %>'  BackColor="Transparent" BorderWidth="0"/>  
  10.                    </div>   
  11.                 </ItemTemplate>  
  12.             </asp:TemplateField>  
  13.         </Columns>  
  14.     </asp:GridView>  
 
  1. protected void Page_Load(object sender, EventArgs e)  
  2.        {  
  3.            if (!this.IsPostBack)  
  4.            {  
  5.                this.Populate();  
  6.            }  
  7.        }  
  8.        private void Populate()  
  9.        {  
  10.            string constr = ConfigurationManager.ConnectionStrings["dbcon1"].ConnectionString;  
  11.            using (SqlConnection con = new SqlConnection(constr))  
  12.            {  
  13.                using (SqlCommand cmd = new SqlCommand("SELECT * FROM loguser", con))  
  14.                {  
  15.                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))  
  16.                    {  
  17.                        DataSet ds = new DataSet();  
  18.                        da.Fill(ds);  
  19.                        GridView1.DataSource = ds;  
  20.                        GridView1.DataBind();  
  21.                    }  
  22.                }  
  23.            }  
  24.        }  
  25.   
  26.        protected void ChangeStatus(object sender, EventArgs e)  
  27.        {  
  28.            string constr = ConfigurationManager.ConnectionStrings["dbcon1"].ConnectionString;  
  29.            Button btn = sender as Button;  
  30.            GridViewRow row = btn.NamingContainer as GridViewRow;  
  31.           int userId = Convert.ToInt32(this.GridView1.DataKeys[row.RowIndex].Value);  
  32.            using (SqlConnection con = new SqlConnection(constr))  
  33.            {  
  34.                using (SqlCommand cmd = new SqlCommand("UPDATE loguser SET Status = @Status WHERE id = @UserId", con))  
  35.                {  
  36.                    cmd.Parameters.AddWithValue("@Status", btn.Text == "Active" ? 0 : 1);  
  37.                    cmd.Parameters.AddWithValue("@UserId", userId);  
  38.                    con.Open();  
  39.                    cmd.ExecuteNonQuery();  
  40.                    con.Close();  
  41.                    Response.Redirect(Request.Url.AbsoluteUri);  
  42.                }  
  43.            }  
  44.        }  
  45.         
 
Output
 
 
My Status is flase or '0' Hide button
 
How to hide Deactive Status Button 
 

Answers (2)