繁体   English   中英

如何绑定数据列表控件的itemtemplate字段内的dropdownlist

[英]how to bind dropdownlist which is inside the itemtemplate field of datalist control

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

您必须处理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"
    >

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM