Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.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# 如何在asp.net ListView控件中填充DropDownList_C#_Asp.net_Listview - Fatal编程技术网

C# 如何在asp.net ListView控件中填充DropDownList

C# 如何在asp.net ListView控件中填充DropDownList,c#,asp.net,listview,C#,Asp.net,Listview,我有一个Listview控件,它在InsertItemTemplate和ItemTemplate中包含dropdownlist控件。 如何填充这些dropdownlist控件? 这是我的标记 <asp:ListView ID="ListView1" runat="server" SelectedIndex="0" InsertItemPosition="LastItem" onitemcommand="ListView3_Item

我有一个Listview控件,它在InsertItemTemplate和ItemTemplate中包含dropdownlist控件。 如何填充这些dropdownlist控件? 这是我的标记

         <asp:ListView ID="ListView1" runat="server" SelectedIndex="0" 
                    InsertItemPosition="LastItem" onitemcommand="ListView3_ItemCommand">
                <LayoutTemplate>
                    <table border="0" cellpadding="1" width="100%" class="formtab">

                        <tr class="header" >
                            <th align="left" width="145px" >Sub Project</th>
                            <th align="left" width="145px">Alloc</th>
                            <th align="left" width="90px">AMT</th>
                            <td align="center" width="30px"></td>
                        </tr>
                        <tr id="itemPlaceholder" runat="server"  style="height:30px;border:1px solid gray;"></tr>
                    </table>
                </LayoutTemplate> 
                <ItemTemplate>
                    <tr class="item" runat="server" id = "thisRow" >
                        <td align="left" >  
                            <asp:DropDownList ID="List2QuoteSubProject" Text='<%# Eval("SubProjectID") %>' runat="server" Width="140px">
                            </asp:DropDownList>
                        </td>
                        <td align="left">
                            <asp:HiddenField ID="HiddenField2" Value='<%# Eval("VendorQuoteAllocationID") %>' runat="server" />
                            <asp:DropDownList ID="List2QuoteAllocation"  Text='<%# Eval("AllocationID") %>'  runat="server" Width="140px">
                            </asp:DropDownList>
                        </td>
                        <td align="left">
                            <asp:TextBox ID="List2Amount" runat="server"  Text='<%# Eval("Amount") %>'  Width="85px">
                            </asp:TextBox>
                        </td>
                          <td align="center" >
                            <asp:ImageButton ID="ImageButton1" runat="server" style="margin-left:8px;margin-right:8px;" ImageUrl="~/Images/icons/remove.png" Width="16" Height="16" />
                        </td>
                    </tr>
                </ItemTemplate>
            </asp:ListView>

子项目
阿洛克
金额

您需要处理ItemDataBound事件:

<asp:ListView ID="ListView1" OnItemDataBound="ListView1_ItemDataBound" runat="server" SelectedIndex="0" 
                InsertItemPosition="LastItem" onitemcommand="ListView3_ItemCommand">
在您最后的评论之后,我编辑代码:

    protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            var dataItem = e.Item.DataItem as SomeType // [The type of object you use for the ListView binding];

            var subpro = e.Item.FindControl("List2QuoteSubProject") as DropDownList;
            BindDropDownList(subpro, "SELECT SubProjectID, SubProjectCode FROM SubProject A INNER JOIN Project B ON A.ProjectID = B.ProjectID ORDER BY B.ProjectCode, A.SubProjectCode", "SubProject", "SubProjectCode", "SubProjectID");

            subpro.SelectedValue = dataItem.SubProjectID;
        }
    }

您可以在填充dropdownlist后设置它的值,不会出现错误

此事件在数据绑定后执行。因此,如果我在绑定时将文本设置为dropdownlist,它将引发错误,因为dropdownlist尚未填充。在哪里填充dropdownlist?在ListView1_ItemDataBound中。我希望在每个rowobject上绑定dropdownlist _subo=e.Item.FindControl(“List2QuoteSubProject”);如果(_subpo!=null&&u subpo是DropDownList){DropDownList subpo=(DropDownList)_subpo;BindDropDownList(subpo),则选择subprojectd,SubProjectCode从子项目A内部加入A.projectd=B.projectd按B.ProjectCode、A.SubProjectCode、SubProjectCode排序,“SubProject”,“SubProjectCode”,“subprojectd”);}很抱歉,我无法更新问题,因为它显示错误,我的问题包含太多代码。
    protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            var dataItem = e.Item.DataItem as SomeType // [The type of object you use for the ListView binding];

            var subpro = e.Item.FindControl("List2QuoteSubProject") as DropDownList;
            BindDropDownList(subpro, "SELECT SubProjectID, SubProjectCode FROM SubProject A INNER JOIN Project B ON A.ProjectID = B.ProjectID ORDER BY B.ProjectCode, A.SubProjectCode", "SubProject", "SubProjectCode", "SubProjectID");

            subpro.SelectedValue = dataItem.SubProjectID;
        }
    }