【问题标题】:ListView find dropdownlist in tableListView 在表中查找下拉列表
【发布时间】:2012-07-08 20:08:37
【问题描述】:

我有一个包含表格的列表视图,我需要获取所有下拉列表和文件上传控件,但 find 没有返回任何内容。这是我的代码:

<asp:ListView runat="server" ID="MyListView" OnItemDataBound="FillDropDownList">
    <LayoutTemplate>
        <table border="0" cellpadding="2" cellspacing="0">
        <tr>
        <th>Wholesaler</th>
        <th>Import</th>
        <th>Period</th>
        <th>Upload Date</th>
        <th>Upload</th>
        </tr>
            <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
    </table>
    </LayoutTemplate>
    <ItemTemplate>
    <tr class="row1">
        <td><%# DataBinder.Eval(Container.DataItem, "Wholesaler") %></td>
            <td><%# DataBinder.Eval(Container.DataItem, "Import")%></td>
        <td><%# DataBinder.Eval(Container.DataItem, "Period")%></td>
        <td><asp:DropDownList runat="server" ID="DaysDropDownList"></asp:DropDownList></td>
       <td><asp:FileUpload ID="FileUpload" runat="server" /></td>
    </tr>
    </ItemTemplate>
</asp:ListView>

DropDownList dr = (DropDownList)MyListView.Controls[0].FindControl("DaysDropDownList");
FileUpload fl = (FileUpload)MyListView.Controls[0].FindControl("FileUpload");

【问题讨论】:

    标签: asp.net listview findcontrol


    【解决方案1】:

    想...你得到了那个错误,因为列表视图还没有绑定,所以我认为最好的方法是在 ItemDataBound 事件上完成所有这些。您会发现下拉列表如下:

    protected void FillDropdownlist(object sender, ListViewItemEventArgs e)
        {
    
         if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                DropDownList dr = (DropDownList)e.Item.FindControl("DaysDropDownList");
                FileUpload fl = (FileUpload)e.Item.FindControl("FileUpload");
    
                if (dr!= null)
                {
                    //code here
                }
            }
    }
    

    【讨论】:

      【解决方案2】:

      您需要遍历列表视图的Items 集合,然后在每个项目上使用FindControl。这样的事情应该会让你走上正轨:

      foreach (var lvItem in MyListView.Items)
      {
          if (lvItem.ItemType == ListViewItemType.DataItem)
           {
              DropDownList dr = (DropDownList)lvItem.FindControl("DaysDropDownList");
              FileUpload fl = (FileUpload)lvItem.FindControl("FileUpload");
          }
      }
      

      【讨论】:

      • 太棒了,这非常适合我的情况。谢谢。
      【解决方案3】:

      那是因为MyListView.Controls[0] 指向一个不包含这两个的内部控件。

      尝试调试并准确找到您的容器控件,然后直接访问它而无需硬编码索引。可以通过行绑定调用的事件参数访问它。

      我还建议您使用as 运算符,因为它不会引发异常:

      as 运算符类似于强制转换,只是它在转换失败时产生 null 而不是引发异常

      DropDownList dr = e.Item.FindControl("DaysDropDownList") as DropDownList;
      FileUpload fl = e.Item.FindControl("FileUpload") as FileUpload;
      

      或绑定后

      //Loop i for the items of your listview
      DropDownList dr = MyListView.Items[i].FindControl("DaysDropDownList") as DropDownList;
      FileUpload fl = MyListView.Items[i].FindControl("FileUpload") as FileUpload;
      

      【讨论】:

        猜你喜欢
        • 2013-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-23
        相关资源
        最近更新 更多