如何解决如何生成和处理多个复选框
| 大师, 我一直在寻找一个清晰的示例,说明如何使用ASP.NET中的C#处理多个不同类别的项目。数据类似于以下内容:Category1 heading
Item
Item
Item
Category2 heading
Item
Item
Item
Category3 heading
Item
Item
Item
类别名称和项目来自sql数据库。我想象每个项目类别都需要一个复选框,并且类别数是动态的,并且会随着时间而变化。是否可以创建一个循环来动态构建所需的复选框列表,然后对其进行处理?我读到有关使用数据转发器的信息。非常感谢您的协助。
解决方法
您可以使用中继器或DataGrid。有时候,对于我来说,使用DataGrid并为每个新Category添加行更加容易。您打算如何处理它吗?
您可以使用DataGrid,并为每个类别添加一个新行。在该行中添加控件。较不熟练的方法是在页面上使用PlaceHolder并向其中添加控件。但是我绝对会建议使用DataGrid或研究Repeater。
, 您可以尝试这样的事情...
您的业务对象。您可以将其用作模型,以从\'real \'业务对象转换或直接使用业务对象
public class BusinessObject
{
public string Category { get; set; } //Your category
public int ID { get; set; } //Point of data entry and will be return on post
public string Name { get; set; } //A friendly name for your users
}
您的aspx标记。我正在为类别使用中继器,其中只有2英镑,其中包含实际项目。可以扩展和设置很多样式。
<asp:Repeater ID=\"myRepeater\" runat=\"server\">
<ItemTemplate>
<asp:CheckBoxList ID=\"checkboxlist\"
runat=\"server\"
DataTextField=\"Name\"
DataValueField=\"ID\" />
</ItemTemplate>
</asp:Repeater>
从中获取业务对象的地方:在这里,我的代码中只有一个成员。您应该从业务层/层中获取此数据。
List<BusinessObject> MyBusinessObjects = new List<BusinessObject>();
还有你的代码
protected void Page_Load(object sender,EventArgs e)
{
//Wire up the event to handle when items are bound to the repeater
this.myRepeater.ItemDataBound += new RepeaterItemEventHandler(myRepeater_ItemDataBound);
//Now actually bind the categories to the repeater
this.myRepeater.DataSource = GetCategories(MyBusinessObjects);
this.myRepeater.DataBind();
}
void myRepeater_ItemDataBound(object sender,RepeaterItemEventArgs e)
{
//Don\'t process items that are not item,or alternating item
if (!(e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)) return;
//Grab a reference to the checkboxlist in our repeater
CheckBoxList checkboxlist = (CheckBoxList)e.Item.FindControl(\"checkboxlist\");
//Now put our business objects of that category in it
checkboxlist.DataSource = GetItemsFromCategory(MyBusinessObjects,(string)e.Item.DataItem);
checkboxlist.DataBind();
}
//Helper to grab categories.
private IEnumerable<string> GetCategories(IEnumerable<BusinessObject> items)
{
return (from i in items
select i.Category).Distinct();
}
//Helper to grab the items in categories.
private IEnumerable<BusinessObject> GetItemsFromCategory(IEnumerable<BusinessObject> items,string category)
{
return (from i in items
where i.Category == category
select i);
}
, 如果您事先不知道可能有多少个物品,那么您需要一个不会损坏的显示器。实现此目的的最佳方法是将ListView与GroupTemplate一起使用:
http://forums.asp.net/t/1364813.aspx/1
, 试想一下,这就是您的数据结构。没有中继器就可以做。
var list = new字典<字符串,列表<字符串>>
{
{\“ Name1 \”,新列表{\“ item1 \”,\“ item2 \”,\“ item3 \”}}},
{\“ Name2 \”,新列表{\“ item1 \”,\“ item2 \”,\“ item3 \”}}},
{\“ Name3 \”,新列表{\“ item1 \”,\“ item2 \”,\“ item3 \”}}},
{\“ Name4 \”,新列表{\“ item1 \”,\“ item2 \”,\“ item3 \”}}},
{\“ Name5 \”,新列表{\“ item1 \”,\“ item2 \”,\“ item3 \”}}
};
foreach(列表中的var类别)
{
var checkBoxList =新的CheckBoxList
{
文字= category.Key
};
foreach(类别值中的变量值)
{
var listItem = new ListItem(value);
checkBoxList.Items.Add(listItem);
}
}
, 再次感谢大家的帮助。我们最终更改了要求,现在只需一个复选框即可。我认为将复选框列表动态添加到占位符还是可行的。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。