Question

I have a list view with table inside and i need to get all dropdown lists and file upload controls, but find returns nothing. This is my code:

<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");
Was it helpful?

Solution

You need to iterate over the Items collection of the listview, and then use FindControl on each item. Something like this should put you on the right track:

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

OTHER TIPS

figured...and you got that error, because the listview was not binded yet, so i think the best way would be to do all this on the ItemDataBound event. You would find the dropdownlist like:

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

That's because MyListView.Controls[0] points to an inner control that does not contain those two.

Try debugging and finding precisely which is your container control, and then accessing it directly without a hard coded index. It's accessible via the event parameter of your row binding calls.

Also might I suggest you use the as operator as it doesn't raise an exception:

The as operator is like a cast except that it yields null on conversion failure instead of raising an exception

i.e.

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

or after it's bound

//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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top