【发布时间】:2017-08-05 01:48:15
【问题描述】:
我在 C# 中有一个 Datalist,其中包含很多复选框和名字。单击按钮时,首先,表中的所有数据都将被删除,并且对于选中的每个复选框,ID 都会再次插入表中。效果很好。
问题是当我只显示某些复选框时,例如名字是 John。显示 ID 的所有复选框,但只有包含 John 的复选框可见。如果我再检查几个复选框并单击按钮,所有行都会再次被删除,然后我的想法是可见的复选框 = false;仍然在那里并且正在再次插入。但他们不是……
有没有其他方法可以让它像我想要的那样工作?
protected void Page_Load(object sender, EventArgs e)
{
GetEmployee();
CompareEmployee();
}
private void GetEmployee()
{
string FirstName = table.Rows[i]["FirstName"].ToString();
string qs = (Request.QueryString["search"].ToString());
DataTable table = SearchPerson(search);
for (int i = 0; i < table.Rows.Count; i++)
{
CheckBox checkbox = new CheckBox();
checkbox.ID = table.Rows[i]["UserID"].ToString();
checkbox.Visible = false;
HyperLink hlFirstName = new HyperLink();
hlFirstName.Text = table.Rows[i]["FirstName"].ToString();
hlFirstName.Visible = false;
string FirstName = table.Rows[i]["FirstName"].ToString();
string qs = (Request.QueryString["search"].ToString());
if (qs.Contains(FirstName))
{
checkbox.Visible = true;
hlFirstName.Visible = true;
}
phEmployee.Controls.Add(checkbox);
phEmployee.Controls.Add(hlFirstName);
}
}
private void CompareEmployee()
{
int UserID = ((User)Session["LoggedInUser"]).UserID;
DataTable table = new DataTable();
table = GetEmployee(UserID);
foreach (Control c in phEmployee.Controls)
{
if (c.GetType() == typeof(CheckBox))
{
for (int i = 0; i < table.Rows.Count; i++)
{
if (c.ID == table.Rows[i][0].ToString())
{
(c as CheckBox).Checked = true;
}
}
}
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
int UserID = ((User)Session["LoggedInUser"]).UserID;
{
DeleteEmployee(UserID);
foreach (Control c in phEmployee.Controls)
{
if (c.GetType() == typeof(CheckBox))
{
if ((c as CheckBox).Checked)
{
AddEmployee(c.ID, UserID);
}
}
}
Response.Redirect("Default.aspx");
}
}
【问题讨论】:
-
检查复选框的
view state