简体   繁体   中英

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

i have two tables which stores items and itemsizes, a item will have different sizes. i am trying to display the itemsizes for the item in a dropdownlist with in a datalist. my html is as below

<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();
        }

i have set up a break point at the foreach loop and checked, it is not entering the loop. any help is appreciated.

You have to handle the DataList.ItemDataBound event:

Assuming you are binding your DataList like: (or similar, using a data source control)

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.dl.DataSource = GetDataSource();
        this.dl.DataBind();
    }
}

Then your ItemDataBound handler would look like:

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();
    }
}

Finally make sure to register the event in the DataList markup

    <asp:DataList ID="dl" runat="server" 
        onitemdatabound="dl_ItemDataBound"
        DataKeyField="Your_Row_ID"
    >

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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