[英]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;"/>
</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 : 一切正常,只是问题在于数据列表包装在updatepannel中,因此为了在用户端下载图像,我需要在Page_Load事件上注册控件:
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."
由于dtlImgDownload
在数据列表中,因此我总是收到错误消息"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. 我尝试了几种查找控件的方法,例如dtlSearchDetails.FindControl("dtlImgDownload ")
但它始终返回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. 请指出正确的方向。
Firstly, you need to Bind
the DataList
. 首先,您需要Bind
DataList
。 Then after this, you will need to loop through each of the DataListItems
in the DataList
然后,在此之后,您将需要遍历DataListItems
中的每个DataList
foreach ( DataListItem li in dtlSearchDetails.Items )
{
ImageButton imgButton = (ImageButton) li.FindControl("dtlImgDownload");
}
This will find the control within each DataListItem
这将在每个DataListItem
找到控件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.