我有两个表存储商品和商品,一个商品有不同的尺寸。我正在尝试在数据列表中的下拉列表中显示项目的项目大小。我的html如下
<asp:DataList ID="dlstCartItems" runat="server" RepeatDirection="Horizontal" RepeatColumns="5"
Width="580px" ForeColor="Blue" DataKeys="ItemCode"
onitemdatabound="dlstCartItems_ItemDataBound">
<ItemTemplate>
<table cellpadding="0" cellspacing="0" style="border: Solid 2px #eeeeee;">
<tr>
<td align="left" style="width: 180px;">
<a href="Videos.aspx?vid=<%# Eval("itemid") %>">
<img src="images/Gallery/<%# Eval("imagename") %>" alt="Image" height="120px" width="185px"
style="border: 0;" />
</a>
</td>
</tr>
<tr>
<td style="width: 180px;" align="left">
<table>
<tr>
<td>
Name:
</td>
<td>
<a href="Videos.aspx?vid=<%# Eval("itemid") %>">
<asp:Label ID="lblVideoName" runat="server" Text='<%# Eval("name")%>' ForeColor="#06C"></asp:Label>
</a>
</td>
</tr>
<tr>
<td>
Author:
</td>
<td>
<a href="Videos.aspx?Authorid=<%# Eval("itemid") %>">
<asp:Label ID="Label1" runat="server" Text='<%# Eval("thm") %>' ForeColor="#06C"></asp:Label></a>
</td>
</tr>
<tr>
<td>
Technology:
</td>
<td>
<a href="Videos.aspx?Techid=<%# Eval("itemid") %>">
<asp:Label ID="Label2" runat="server" Text='<%# Eval("itemcode") %>' ForeColor="#06C"></asp:Label></a>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="ddlAvailableSizes" runat="server" >
</asp:DropDownList>
</td>
</tr>
</table>
</ItemTemplate>
<%--<AlternatingItemStyle BackColor="Silver" />--%>
<ItemStyle BackColor="#eeeeee" />
</asp:DataList>
This is my bind method
var query = (from b in db.CR_CartItems
join c in db.CR_CartItems_Sizes on b.ItemCode equals c.ItemCode
join k in db.CR_ScrollingMenus on b.ThemeID equals k.MenuID
where b.status == true
orderby b.updatedat descending
select new
{
itemid = b.Itemid,
imagename = b.galleryimg,
itemcode = b.ItemCode,
thm = k.DisplayName,
name = b.posterName
}).Distinct();
foreach (var q in query)
{
var query1 = from b in db.CR_CartItems_Sizes
join c in db.CR_CartItems on b.ItemCode equals c.ItemCode
where b.ItemCode == q.itemcode
select new
{
b.SizeDesc,
b.ItemCode
};
foreach (DataListItem li in dlstCartItems.Items)
{
DropDownList list = (DropDownList)li.FindControl("ddlAvailableSizes");
list.DataSource = query1;
list.DataTextField = "SizeDesc";
list.DataValueField = "ItemCode";
list.DataBind();
list.Items.Insert(0, "Available Sizes");
}
}
dlstCartItems.DataSource = query;
dlstCartItems.DataBind();
}
我在foreach循环中设置了一个断点并检查,它没有进入循环。任何帮助表示赞赏。
答案 0 :(得分:1)
您必须处理DataList.ItemDataBound
事件:
假设您绑定DataList
之类的:(或类似的,使用数据源控件)
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.dl.DataSource = GetDataSource();
this.dl.DataBind();
}
}
然后你的ItemDataBound
处理程序看起来像:
protected void dl_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var myDropDownList = e.Item.FindControl("YourDropDownListID") as DropDownList;
int currentItemID = int.Parse(this.dl.DataKeys[e.Item.ItemIndex].ToString());
myDropDownList.DataSource = GetDDLDataSource(currentItemID);
myDropDownList.DataBind();
}
}
最后确保在DataList
标记
<asp:DataList ID="dl" runat="server"
onitemdatabound="dl_ItemDataBound"
DataKeyField="Your_Row_ID"
>