我有一个列表,例如:
<ul id="list">
<li id="content_rptGalleryImages_galleryListItem_0" data-imageid="7" data-displayorder="0"></li>
<li id="content_rptGalleryImages_galleryListItem_1" data-imageid="5" data-displayorder="1"></li>
<li id="content_rptGalleryImages_galleryListItem_2" data-imageid="4" data-displayorder="2"></li>
</ul>
我希望能够访问每个列表项data-imageid
和data-displayorder
属性,并将这些属性添加到我的代码后面的字典中,例如: -
Dictionary<int, int> dict = new Dictionary<int, int>();
foreach(var li in list.Controls)
{
//find li
dict.Add(li.Attribute["data-imageid"], li.Attribute["data-displayorder"]);
}
我想实现的目标是什么?如果没有,你能提出另一种方法吗?
根据请求编辑*转发器代码:
protected void rptGalleryImages_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
BrandImage image = (BrandImage)e.Item.DataItem;
HyperLink lnkGalleryImage = (HyperLink)e.Item.FindControl("lnkGalleryImage");
HyperLink lnkImageUrl = (HyperLink)e.Item.FindControl("lnkImageUrl");
LinkButton lnkBtnRemoveImg = (LinkButton)e.Item.FindControl("lnkBtnRemoveImg");
HtmlGenericControl galleryListItem = (HtmlGenericControl)e.Item.FindControl("galleryListItem");
lnkGalleryImage.Text = image.ImageLocation.Substring(image.ImageLocation.LastIndexOf("/")).Replace("/", "");
lnkGalleryImage.NavigateUrl = image.ImageLocation;
lnkImageUrl.Text = image.ImageUrl;
lnkImageUrl.NavigateUrl = image.ImageUrl;
lnkBtnRemoveImg.CommandArgument = image.ImageLocation;
galleryListItem.Attributes.Add("data-imageid", image.Id.ToString());
galleryListItem.Attributes.Add("data-displayorder", image.DisplayOrder.ToString());
}
}
由于
答案 0 :(得分:1)
浏览器不会在帖子请求中发送li
元素属性,因为它不被视为表单帖子变量。这使得在用户保存时无法在服务器上检索属性值。
使其运作的一个选项: