This Way:
HTML:
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="UserId">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnChangeStatus" runat="server" OnClick="ChangeStatus" Text='<%# Eval("Status").ToString() == "True" ? "Active" : "De Activate" %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
C#:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.Populate();
}
}
private void Populate()
{
string constr = ConfigurationManager.ConnectionStrings["ConString2"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM UserInformation", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}
}
protected void ChangeStatus(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["ConString2"].ConnectionString;
Button btn = sender as Button;
GridViewRow row = btn.NamingContainer as GridViewRow;
int userId = Convert.ToInt32(this.GridView1.DataKeys[row.RowIndex].Value);
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("UPDATE UserInformation SET Status = @Status WHERE UserId = @UserId", con))
{
cmd.Parameters.AddWithValue("@Status", btn.Text == "Active" ? 0 : 1);
cmd.Parameters.AddWithValue("@UserId", userId);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect(Request.Url.AbsoluteUri);
}
}
}
SQL:
CREATE TABLE [dbo].[UserInformation](
[UserId] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](20) NOT NULL,
[Status] [bit] NOT NULL,
CONSTRAINT [PK_UserInformation] PRIMARY KEY CLUSTERED
(
[UserId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
INSERT Query :
INSERT INTO [Sample].[dbo].[UserInformation]
([Name]
,[Status])
VALUES
('Jhon'
,0)
GO
INSERT INTO [Sample].[dbo].[UserInformation]
([Name]
,[Status])
VALUES
('Kabir'
,1)
GO
This is for changing User status now you have to check the UserStatus on PostBack or the Page load first Time where the user is active ir not active.
Thank You.