【问题标题】:How to fill a DropDownList inside asp.net ListView control如何在 asp.net ListView 控件中填充 DropDownList
【发布时间】:2015-05-03 10:25:10
【问题描述】:

我有一个 Listview 控件,它在其 InsertItemTemplate 和 ItemTemplate 中包含下拉列表控件。 如何填写那些下拉列表控件? 这是我的标记。

         <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>

【问题讨论】:

    标签: c# asp.net listview


    【解决方案1】:

    您需要处理 ItemDataBound 事件:

    <asp:ListView ID="ListView1" OnItemDataBound="ListView1_ItemDataBound" runat="server" SelectedIndex="0" 
                    InsertItemPosition="LastItem" onitemcommand="ListView3_ItemCommand">
    

    然后在后面的代码中,您可以使用 FindControl 获取每个 DropDownList 对象的实例:

        protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                var List2QuoteSubProject = e.Item.FindControl("List2QuoteSubProject") as DropDownList;
            }
        }
    

    在您的最后评论之后,我编辑了代码:

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

    你可以在填充后设置下拉列表的值,不会报错

    【讨论】:

    • 该事件在数据绑定后执行。因此,如果我在绑定时将 Text 设置为下拉列表,则会引发错误,因为下拉列表尚未填充。
    • 你在哪里填充下拉列表?
    • 在 ListView1_ItemDataBound 内。我想在每一行上绑定下拉列表
    • object _subpro = e.Item.FindControl("List2QuoteSubProject"); if (_subpro != null && _subpro 是 DropDownList) { DropDownList subpro = (DropDownList)_subpro; 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"); }
    • 对不起。我无法更新问题,因为它显示我的问题代码过多的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 2016-05-08
    相关资源
    最近更新 更多