2

我有两个存储项目和项目大小的表,一个项目将有不同的大小。我正在尝试在数据列表中显示下拉列表中项目的项目大小。我的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 循环中设置了一个断点并检查,它没有进入循环。任何帮助表示赞赏。

4

1 回答 1

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"
    >
于 2012-07-29T03:43:56.613 回答