我的 asp.net 页面中有一个数据列表。我在代码隐藏中将数据源绑定到它 我在此数据列表中有一个复选框。
var n = from gi in DataContext.Context.GalleryImages
join g in DataContext.Context.Galleries
on gi.GalleryID equals g.GalleryID
where g.UserID == UserID && gi.GalleryID==GalleryID
select new
{
GalleryID = g.GalleryID,
ImageDescription = gi.ImageDescription,
GalleryName = g.GalleryName,
ImageFileName = gi.ImageFileName,
IsAlbumImage = gi.IsAlbumImage,
ImageID=gi.ImageID
};
dlGalleryList.DataSource = n;
dlGalleryList.DataBind();
当“IsAlbumImage”为真时,应选中该复选框。 如何将此属性绑定到复选框?
它应该像这样绑定:
<ItemTemplate>
<asp:CheckBox id="MyCheckBox" runat="server" Checked='<%#Eval("IsAlbumImage") %>' />
</ItemTemplate>
实际上你有办法在数据列表中绑定复选框 1-(推荐)使用 Bind 或 Eval 直接从 ASP 代码绑定它
<ItemTemplate>
<asp:CheckBox id="MyCheckBox" runat="server" Checked='<%#Eval("IsAlbumImage") %>' />
</ItemTemplate>
2- 将其绑定到 ItemDataBound 事件
首先,您将事件处理程序添加到数据列表控件,并将布尔值添加到要在 itemdatabound 事件中使用的数据键
<asp:DataList ID = "DataList1" OnItemDataBound="DataListItemEventHandler" DataKeys = "IsAlbumImage"/>
然后添加绑定此的 C# 代码
protected void DataListItemEventHandler(object sender, DataListItemEventArgs e)
{
CheckBox checkbx = new CheckBox();
checkbx = (CheckBox)e.Item.FindControl("MyCheckBox");
checkbx.Checked = (bool) DataList1.DataKeys(e.Item.ItemIndex)("IsAlbumImage");
}
像这样:
<asp:CheckBox
ID="check"
runat="server"
Checked='<%# Eval("column_name").ToString().Equals("1") %>'
/>