如何在ASP.NET中创建动态复选框 [英] How to create dynamic checkbox in asp.net
问题描述
我正在创建一个需要添加动态复选框列表的应用程序.请任何人告诉我如何使用C#添加动态复选框列表.
I am creating an application where I require to add dynamic checkbox list. Please anyone tell me how to add dynamic checkbox list using C#.
推荐答案
在表单上放置一个ID为"placeHolder"的placeHolder,并将以下代码添加到您的Page_Load()...
Put a placeHolder on your form with the ID "placeHolder" and add the following code to your Page_Load()...
CheckBoxList cbList = new CheckBoxList();
for (int i = 0; i < 10; i++)
cbList.Items.Add(new ListItem("Checkbox " + i.ToString(), i.ToString()));
placeHolder.Controls.Add(cbList);
这将在CheckBoxList(cbList)中添加10个CheckBox对象.
This will add 10 CheckBox objects within your CheckBoxList(cbList).
使用以下代码检查CheckBoxList中的每个CheckBox对象
Use the following code to examine each CheckBox object within the CheckBoxList
foreach(ListItem li in cbList.Items)
{
var value = li.Value;
var text = li.Text;
bool isChecked = li.Selected;
}
占位符用于在运行时将CheckBoxList添加到表单中,使用占位符可以让您更好地控制CheckBoxList及其项在网页中的显示位置
The placeholder is used to add the CheckBoxList to the form at runtime, using a placeholder will give you more control over where in the web page the CheckBoxList and its items will appear
这篇关于如何在ASP.NET中创建动态复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!