我在C#中有很多复选框和名字的数据。当单击一个按钮时,首先,表中的所有数据都将被删除,并且对于每个选中的复选框,该ID都会再次插入tabel中。这很好。checkbox.Visible = false;但它在那里?
问题是,当我只显示某些复选框,例如firstname是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");
}
}
检查CheckBox的'view state' – 2012-01-09 18:17:23