I want to create a situation where on a click of a button a user account will be suspended. I have a checkbox as HeaderTemplate and checkboxes inside ItemTemplate. All these checkboxes are inside a gridview where users are displayed.
An Admin can suspend multiple rows with checkbox selection and click of button.
I tried following an article on Delete multiple rows in GridView with CheckBox selection and with confirmation in ASP.Net but in place of delete, I used update to update the users table column for suspension. But I got an error:
Object reference not set to an instance of an object.
CheckBox chkAll = (CheckBox)UsersGrid.HeaderRow.Cells[0].FindControl("chkAll");
HTML
<asp:GridView ID="UsersGrid" runat="server" GridLines="None" AllowPaging="true" HeaderStyle-ForeColor="#224f6d" HeaderStyle-Font-Bold="true"
AutoGenerateColumns="false" OnPageIndexChanging="OnPageIndexChanging" DataKeyNames="Uid" PageSize="7" CssClass="table" Width="100%" OnRowDataBound="OnRowDataBound" HeaderStyle-HorizontalAlign="left" RowStyle-HorizontalAlign="Left">
<EmptyDataTemplate>
<div>
<asp:Label ID="labelTemp" runat="server" Text="0 Users"></asp:Label>
</div>
</EmptyDataTemplate>
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" onclick="checkAll(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="checker" runat="server" onclick="All_Check(this);" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Uid" HeaderText="User ID" />
<asp:BoundField DataField="Name" HeaderText="Organization Name" />
<asp:BoundField DataField="email" HeaderText="Email" ReadOnly="true" />
<asp:BoundField DataField="Role" HeaderText="User Role" ReadOnly="true" />
<asp:BoundField DataField="IsActive" HeaderText="User Status" ReadOnly="true" />
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<div class="badge badge-pill badge-success" runat="server" id="Active">Active</div>
<div class="badge badge-pill badge-warning" runat="server" id="InActive">InActive</div>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnsuspend" runat="server" CssClass="btn btn-success" Font-Bold="true" Font-Size="10pt" Text="Suspended" OnClientClick="return ConfirmSuspend();" OnClick="btnsuspend_Click" />
Code
SqlCommand cmd = new SqlCommand();
SqlDataAdapter sda = new SqlDataAdapter();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True");
private int PageSize = 4;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.GetData();
this.AllClientsPage();
}
}
private void AllClientsPage()
{
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True");
string query = "SELECT Uid, Name, email, Role, IsActive FROM Users";
SqlDataAdapter sda = new SqlDataAdapter(query, con);
DataTable dt = new DataTable();
sda.Fill(dt);
UsersGrid.DataSource = dt;
UsersGrid.DataBind();
}
private void GetData()
{
ArrayList arr;
if (ViewState["SelectedRecords"] != null)
arr = (ArrayList)ViewState["Selectedrecords"];
else
arr = new ArrayList();
CheckBox chkAll = (CheckBox)UsersGrid.HeaderRow.Cells[0].FindControl("chkAll");
for (int i = 0; i < UsersGrid.Rows.Count; i++)
{
if (chkAll.Checked)
{
if (!arr.Contains(UsersGrid.DataKeys[i].Value))
{
arr.Add(UsersGrid.DataKeys[i].Value);
}
}
else
{
CheckBox checker = (CheckBox)UsersGrid.Rows[i].Cells[0].FindControl("checker");
if (checker.Checked)
{
if (!arr.Contains(UsersGrid.DataKeys[i].Value))
{
arr.Add(UsersGrid.DataKeys[i].Value);
}
}
else
{
if (arr.Contains(UsersGrid.DataKeys[i].Value))
{
arr.Remove(UsersGrid.DataKeys[i].Value);
}
}
}
}
ViewState["SelectedRecords"] = arr;
}