mercredi 4 mars 2015

How to get dynamically created checkboxes' value

I have a Users table in my Webforms ASP.NET 4.5 application which is filled dynamically with users. In that table there are checkboxes before the each username. I want to delete all the users which i checked after i click a delete button. However, when i click delete button, although button clicked event is called, checkedchanged event of checked checkboxes is not called. How can i get these checked checkboxes' value when i click the delete button?



using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication3.Account
{
public partial class Register : System.Web.UI.Page
{
private UserStore<IdentityUser> userStore;
private UserManager<IdentityUser> userManager;
private List<string> users = new List<string>();
protected void Page_Load(object sender, EventArgs e)
{
userStore = new UserStore<IdentityUser>();
userManager = new UserManager<IdentityUser>(userStore);
if (!IsPostBack)
{
fillTable();
}
}

private void fillTable()
{
TableHeaderRow headerRow = new TableHeaderRow();
TableHeaderCell hCell1 = new TableHeaderCell();
hCell1.CssClass = "col-md-1";
hCell1.Text = "#";
TableHeaderCell hCell2 = new TableHeaderCell();
hCell2.Text = "Kullanıcı Adı";
hCell2.CssClass = "col-md-5";
headerRow.Controls.Add(hCell1);
headerRow.Controls.Add(hCell2);
headerRow.Controls.Add(new TableHeaderCell() { CssClass = "col-md-3" });
headerRow.Controls.Add(new TableHeaderCell() { CssClass = "col-md-3" });
headerRow.TableSection = TableRowSection.TableHeader;
tblUser.Rows.Add(headerRow);

foreach (IdentityUser user in userManager.Users.ToList())
{
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
CheckBox checkBox = new CheckBox();
checkBox.InputAttributes.Add("value", user.Id);
checkBox.CheckedChanged += checkBox_CheckedChanged;
cell1.Controls.Add(checkBox);
cell1.HorizontalAlign = HorizontalAlign.Center;
TableCell cell2 = new TableCell();
cell2.Text = user.UserName;
TableCell cell3 = new TableCell();
HyperLink link = new HyperLink();
link.Text = "Şifreyi Değiştir";
cell3.Controls.Add(link);
row.Controls.Add(cell1);
row.Controls.Add(cell2);
row.Controls.Add(new TableCell());
row.Controls.Add(cell3);
tblUser.Rows.Add(row);
row.TableSection = TableRowSection.TableBody;
}
}

void checkBox_CheckedChanged(object sender, EventArgs e)
{
if (ViewState["userList"] != null)
{
users = (List<string>)ViewState["userList"];
}
CheckBox checkbox = (CheckBox)sender;
if (checkbox.Checked)
{
if (!users.Contains(checkbox.InputAttributes["value"]))
{
users.Add(checkbox.InputAttributes["value"]);
}
}
else
{
if (users.Contains(checkbox.InputAttributes["value"]))
{
users.Remove(checkbox.InputAttributes["value"]);
}
}

ViewState["userList"] = users;
}

protected void btnDeleteUser_Click(object sender, EventArgs e)
{
foreach (string id in users)
{
IdentityUser user = userManager.FindById(id);
userManager.Delete(user);
}
fillTable();
}

}
}

Aucun commentaire:

Enregistrer un commentaire