在 Web 表单中,我动态创建了一系列添加到 asp:placeholder 的复选框
//Generates the List of Entities that may be shared
protected void GenerateEntityList(string agencyId, string agencyName)
{
var selectedAgency = UserFactory.GetAgencyAndSharedDataById(Convert.ToInt64(agencyId));
entityContainer.Controls.Clear();
//builds the entity list based upon entities in DB
var currentEntities = UserFactory.GetEntityList();
//add checkboxes
entityContainer.Controls.Add(new LiteralControl("<input type='checkbox' id='selectAllCB' value='All'>Select All<br/><br/>"));
foreach (var item in currentEntities)
{
CheckBox entityCheckBox = new CheckBox();
int currentItem = item.EntityTypeId.GetValueOrDefault(0);
entityCheckBox.Text = item.EntityName.ToString();
entityCheckBox.CssClass = "am_Checkbox";
entityContainer.Controls.Add(entityCheckBox);
entityContainer.Controls.Add(new LiteralControl("<br/><br/>"));
//mark checkboxes as checked if currently stored in the database
if (selectedAgency.FirstOrDefault().SecurityDataShares != null && selectedAgency.FirstOrDefault().SecurityDataShares.Count > 0)
{
foreach (var ce in selectedAgency.FirstOrDefault().SecurityDataShares)
{
if (ce.EntityId == item.EntityId)
{
entityCheckBox.Checked = true;
}
}
}
}
创建后,用户可以进行或删除选择。单击更新时,我想返回/收集复选框中的值,以便我可以将它们重新插入数据库。
//get the text value of every checkbox
protected void updateEnties()
{
string receivingAgency = ReceivingAgencyID;
long contributingAgency = QueryID;
List<string> cbValues = new List<string>();
foreach (CheckBox currentItem in entityContainer.Controls)
{
cbValues.Add(currentItem.Text);
}
}
但是,当我尝试遍历集合时,我没有得到任何返回,这使我相信我错误地调用了控件,或者它由于回发而被破坏。我应该使用其他控件(如 Listbox 控件)而不是将这些元素写入占位符吗?
有人可以为我提供一些关于如何检索复选框值的指导吗?