Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何绑定datalist控件的itemtemplate字段中的dropdownlist_C#_Asp.net_Drop Down Menu_Datalist - Fatal编程技术网

C# 如何绑定datalist控件的itemtemplate字段中的dropdownlist

C# 如何绑定datalist控件的itemtemplate字段中的dropdownlist,c#,asp.net,drop-down-menu,datalist,C#,Asp.net,Drop Down Menu,Datalist,我有两个表存储项目和项目大小,一个项目将有不同的大小。我试图在数据列表中的下拉列表中显示该项目的项目大小。我的html如下所示 <asp:DataList ID="dlstCartItems" runat="server" RepeatDirection="Horizontal" RepeatColumns="5" Width="580px" ForeColor="Blue" DataKeys="ItemCode"

我有两个表存储项目和项目大小,一个项目将有不同的大小。我试图在数据列表中的下拉列表中显示该项目的项目大小。我的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();
        }

姓名:
作者:
技术:
这是我的绑定方法
var query=(来自db.CR\u中的b)
在b.ItemCode等于c.ItemCode的db.CR\u CartItems\u大小中加入c
在b.ThemeID=k.MenuID上的db.CR_滚动菜单中加入k
其中b.status==true
orderby b.updatedat降序
选择新的
{
itemid=b.itemid,
imagename=b.GalleryMg,
itemcode=b.itemcode,
thm=k.DisplayName,
name=b.posterName
}).Distinct();
foreach(查询中的var q)
{
var query1=以db.CR\u CartItems\u大小表示的b
在db.CR_CartItems中加入c。b.ItemCode等于c.ItemCode
其中b.ItemCode==q.ItemCode
选择新的
{
b、 SizeDesc,
b、 项目代码
};
foreach(dlstCartItems.Items中的DataListItem li)
{
DropDownList list=(DropDownList)li.FindControl(“ddlAvailableSizes”);
list.DataSource=query1;
list.DataTextField=“SizeDesc”;
list.DataValueField=“ItemCode”;
list.DataBind();
列表项目。插入(0,“可用尺寸”);
}
}
dlstCartItems.DataSource=查询;
dlstCartItems.DataBind();
}

我已经在foreach循环中设置了一个断点,并检查它是否没有进入循环。非常感谢您的帮助。

您必须处理
DataList.ItemDataBound
事件:

假设您正在绑定
DataList
like:(或类似,使用数据源控件)

然后您的
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"
    >


您提供的上述代码将向所有下拉列表添加相同的字段,我的要求与上述链接相同。如果图像项在数据库中有两个特定的大小,它应该只显示两个,如果它有4个,则只显示4..这并不难,事实上,我提供的代码只是向您展示了一个示例,我只是更新了它以提取当前项行的ID,并使用它为每一行过滤正确的数据源。这段代码应该足以为您指明正确的方向。如果你有更多的问题,请告诉我。
    <asp:DataList ID="dl" runat="server" 
        onitemdatabound="dl_ItemDataBound"
        DataKeyField="Your_Row_ID"
    >