在一个网页表单我动态创建一系列chekboxes的被添加到一个asp:占位符
Within a web form I am dynamically creating a series of chekboxes that are added to an 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; } } } }在创建的用户必须或删除选择的能力。当点击更新,我想从复选框返回/收集值,这样我可以将它们插入回分贝。
Once created the user has the ability to make or remove selections. When clicking update I would like to return/collect the values from the checkboxes so that I may insert them back into the db.
//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); } }然而,当我试图遍历我没有得到任何东西收集这使我相信,无论我是不正确调用控件或它正在因回传被破坏。而不是写这些元素的占位符我应该使用其他控件像一个列表框控件?
However when I attempt to iterate over the collection I am not getting anything back which leads me to believe either I am calling the control incorrectly or it is being destroyed due to the postback. Instead of writing these elements to a place holder should I use another controls like a Listbox control?
可有人请我提供我如何能检索复选框值一些指导?
Can someone please provide me with some guidance on how I can retrieving the checkbox values?
推荐答案有两个思想流派;一个,那你需要重新被初始视图下所作的控制层次,这样的页面可以恢复视图状态和形式值到控件树,两下,你可以用一个低技术的解决方案去简单地获得从表单收集值。
There are two schools of thought; one, that you need to recreate the control hierarchy that was made for the initial view so that the page can restore the view state and form values into the control tree, or two, that you can go with a low tech solution and simply get the values from the form collection.
我个人,像方案二,它是比较容易处理,而且因为它看起来像你已经手动创建与 LiteralControl 的HTML。只需提供一个名称这是你手动创建的输入标签之间共享属性(这里为简便起见显示例如,这可能不是你会做什么):
I personally, like option two, and it's relatively easy to deal with, and since it looks like you're already manually creating the HTML with LiteralControl. Simply provide a name attribute that is shared among your manually created input tags (example shown here for brevity, this is probably not what you will do):
entityContainer.Controls.Add(new LiteralControl("<input type='checkbox' name='mySharedName' />");然后,把它作为简单的值是:
Then, to get the values it is as simple as this:
var selectedValues = Request.Form["mySharedName"]; // This is now a comma separated list of values that was checked然后,你不必担心控制树的状态。
Then you dont have to worry about the control tree state.
更多推荐
如何从在asp.net中动态生成复选框列表获取复选框值
发布评论