复选框.Visible=false;但它还在吗
本文关键字:Visible false 复选框 | 更新日期: 2023-09-27 18:20:00
我在C#中有一个数据列表,其中有很多复选框和名字。单击按钮时,首先,表中的所有数据都将被删除,对于选中的每个复选框,ID都将再次插入到选项卡中。这很有效。
问题是当我只显示某些复选框时,例如名字是John。显示所有带有ID的复选框,但只有包含John的复选框可见。如果我再选中几个复选框并单击按钮,那么所有行都将被删除,然后我的想法是可见的复选框=false;Stil在那里,正在被再次插入。但他们不是。。。
有没有其他方法可以让它像我想要的那样工作?
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");
}
}
将Visible
设置为false
不会导致复选框消失在空白中,它仍然存在于Controls
集合中。我会避免以这种方式使用UI的状态,而是处理CheckedChanged
事件,并设置一些内部数据结构来告诉你当前的状态
此外,请避免执行两次强制转换。你可以简单地做
var chk = c as CheckBox;
if( c != null )
{
AddEmployee(c.ID, UserID);
}
复选框仍然存在。但它们不一定要经过检查。根据您的描述,听起来不可见的未选中框会导致记录被删除。
在if()
后面有一个;
,基本上结束了该语句:if (qs.Contains(FirstName));
始终执行以下括号。您可能会丢失代码来隐藏复选框,但这不在上面的代码中。
在C#中,我使用了它,它成功了。希望它能有所帮助!
_checkBoxName.Visibility=System.Windows.Visibility.Visible;