我的Webforms ASP.NET 4.5应用程序中有一个Users表,它由用户动态填充。在该表中,每个用户名之前都有复选框。我想删除我点击删除按钮后检查的所有用户。但是,当我单击“删除”按钮时,虽然调用了按钮单击事件,但未调用已检查复选框的checkedchanged事件。如何获得这些选中的复选框'单击删除按钮时的值?
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();
}
}
}
答案 0 :(得分:0)
试试这个:
foreach(TableRow row in tblUser)
{
foreach(TableCell cell in row)
{
foreach(Control ctrl in cell)
{
if (ctrl.GetType() == CheckBox)
{
/* get value here */
}
}
}
}
答案 1 :(得分:0)
protected void btnDeleteUser_Click(object sender, EventArgs e)
{
foreach(var row in tblUser.Rows)
{
var tempcheckBox= row.Controls[0] as CheckBox;
if(tempcheckBox.IsChecked)
{
//your code
}
}
}
row.Controls [0]因为复选框已插入第一列内
在Page_LoadComplete
方法中移动dataBinding流程
删除测试if(!IsPostBack)
。
事实上,如果你调用一个postBack,你首先要通过Pre_Init,PageLoad,你的EventHandler,然后是Page_Load Complete。
因此,如果您的绑定过程是在Page_Load中完成的。自动您将在您的eventHandler中找到gridView,因为它是在PageLoad中初始化的。
答案 2 :(得分:0)
从您创建动态复选框列表的foreach循环中删除以下行:
checkBox.CheckedChanged += checkBox_CheckedChanged;
请添加以下行而不是以上行:
checkBox.Attributes.Add("name", "chkDel");
checkBox.Attributes.Add("onclick", "UncheckDelAll('chkDelAll','chkDel')");
在页面顶部放下javascript:
function UncheckDelAll(HeaderChk, RowChk) {
var cnt = 0; //Count of all the check boxes
objSelected = document.getElementsByName(HeaderChk)
obj = document.getElementsByName(RowChk)
if (obj != null) {
cnt = obj.length;
for (var i = 0; i < cnt; i++) {
if (obj[i].checked == true);
cnt = 1;
}
if (cnt == 1)
objSelected[0].checked = false;
}
}
这是删除按钮单击事件逻辑:
protected void lkDelete_Click(object sender, EventArgs e)
{
string sPrimaryIds = (string)Request.Params["chkDel"];
// Write your delete query functionality over here
// Your delete query like this
// delete from [tablename] where UserId in (sPrimaryIds)
}