foo-baar
foo-baar

Reputation: 1104

How to find a control located inside datalist itemtemplate, on a Page_Load event?

I have a datalist which display's thumbnails of images and a download icon under it, when user clicks on the download icon, system download's the image at client's location.

<asp:DataList ID="dtlSearchDetails" runat="server" OnItemCommand="dtlSearchDetails_ItemCommand" OnItemDataBound="dtlSearchDetails_ItemDataBound">
                            <ItemTemplate>
                                <asp:ImageButton runat="server" ID="dtlImageCol" ImageUrl='<%# "~/uploads/thumbnails/" + Eval("ImageName") %>' /><br />
                                <asp:Label runat="server" ID="dtusage" Text='<%# Eval("usage") %>' Style="color: #CC121B;"></asp:Label><br />
                                <asp:ImageButton runat="server" ID="dtlImgDownload" CommandName="dtlImgDownload" CommandArgument='<%# Eval("ImageName") %>' ImageUrl="images/download.png" style="height:20px; width:20px;"/>&nbsp;
                            </ItemTemplate>
                        </asp:DataList>

All works well just the problem is that datalist is wrapped inside updatepannel and hence in order to download image at user end I need to register the control on the Page_Load event :

ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(dtlImgDownload);

since the dtlImgDownload is inside the datalist, I always get an error "The name dtlImgDownload doesn't exists in current context."

I tried several ways to find the control like dtlSearchDetails.FindControl("dtlImgDownload ") but it always returns null.

I also tried

if(dtlSearchDetails.FindControl("dtlImgDownload ") != null)
        {
            ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(dtlSearchDetails.FindControl("dtlImgDownload "));
        }

But same result, its always null.

Kindly point me to the right direction.

Upvotes: 0

Views: 14979

Answers (1)

Tim B James
Tim B James

Reputation: 20364

Firstly, you need to Bind the DataList. Then after this, you will need to loop through each of the DataListItems in the DataList

foreach ( DataListItem li in dtlSearchDetails.Items )
{
    ImageButton imgButton = (ImageButton) li.FindControl("dtlImgDownload");
}

This will find the control within each DataListItem

Upvotes: 0

Related Questions