我在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