如何从在asp.net中动态生成复选框列表获取复选框值

编程入门 行业动态 更新时间:2024-10-27 08:37:15
本文介绍了如何从在asp中动态生成复选框列表获取复选框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在一个网页表单我动态创建一系列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中动态生成复选框列表获取复选框值

本文发布于:2023-10-12 01:37:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483394.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:复选框   动态   列表   asp   net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!