Solved

Display data in datalist

Posted on 2006-07-10
10
378 Views
Last Modified: 2011-10-03
Hi All!
i have a datalist as below
<asp:datalist id="repPhotoG" runat="server" RepeatDirection="Horizontal">
  <ItemTemplate>

     <a onclick="return enlarge('ThuVienHinhAnh/large_25163.gif',event)"><img      runat=server src='<%# DataBinder.Eval(Container.DataItem, "HinhAnh") %>'  title='<%# DataBinder.Eval(Container.DataItem, "HinhAnh") %>' width="69" height="95" style="cursor:pointer"></a>

</ItemTemplate>
</asp:datalist>
Now I want Replace "ThuVienHinhAnh/large_25163.gif" by <%# DataBinder.Eval(Container.DataItem, "HinhAnh") %> How To?, Thanks
Regards,
0
Comment
Question by:duynnh
  • 4
  • 3
10 Comments
 
LVL 9

Expert Comment

by:valkyrie_nc
ID: 17073537
Best way I see is to add runat="server" to the <a> tag, and then add the onclick event on itemdatabound:

<a id="namedLink" runat="server"><img ... /></a>

In ItemDataBound event:

pull the value of HinhAnh any way you like (maybe store the value in an invisible label), and then

namedLink.Attributes.Add("onclick","return enlarge(" + HinhAnh + ",event);");


hth

valkyrie_nc
0
 

Author Comment

by:duynnh
ID: 17073591
Hi valkyrie_nc!
I didn't find itemdatabound  event of <a> tag
0
 
LVL 9

Expert Comment

by:valkyrie_nc
ID: 17073671
Since you're using a datalist, you would create an ItemDataBound event, tie it to your DataList, and use it.  You might want to turn the <a> tag into an <asp:HyperLink> tag to make it easier to hunt down using FindControl (apologies if you're a VB.Net person; I'm not that great at translating):

<asp:DataList ID="repPhotoG" runat="server" RepeatDirection="Horizontal" OnItemDataBound="repPhotoG_ItemDataBound">...</asp:DataList>

In code-behind:

protected void repPhotoG_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
   if (e.Item.ItemType == System.Web.UI.WebControls.ListItemType.Item || e.Item.ItemType == System.Web.UI.WebControls.ListItemType.AlternatingItem)
   {
      HyperLink namedLink = e.Item.FindControl("namedLink") as HyperLink;
      Label hinhLbl = e.Item.FindControl("hinhLbl") as Label; // if you used a label to store the data for code-behind retrieval; just one way to do it
      string HinhAnh = hinhLbl.Text;
      namedLink.Attributes.Add("onclick","return enlarge(" + HinhAnh + "),event);");
   }
}

Regardless, since you're mixing a DataItem value (HinhAnh) with non-databinder info (return enlarge(value,event)), you'll need to either do it from code-behind, or alternatively if you're using SQL, you can return the value of HinhAnh to have it include "return enlarge(value,event)" as part of its value, and use that without restoring to code-behind.

hth

valkyrie_nc
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:duynnh
ID: 17073746
Hi valkyrie_nc!
I don't use HyperLink, i just use <img> tag
0
 
LVL 9

Expert Comment

by:valkyrie_nc
ID: 17073761
You'd use HyperLink in place of the <a> tag; the <img> tag is fine:

<asp:HyperLink ID="namedLink" runat="server"><img src="file.jpg" ... /></asp:HyperLink>

Make more sense, or less?  I'm trying not to drown you in stuff you already know. :)

hth

valkyrie_nc
0
 

Author Comment

by:duynnh
ID: 17073833
Hi valkyrie_nc!
I have solved my problem as below:
Private Sub repPhotoG_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles repPhotoG.ItemDataBound
     
        Dim img As System.Web.UI.HtmlControls.HtmlImage = e.Item.FindControl("iPhoto")
        img.Attributes.Add("onclick", "return enlarge('" + img.Src + "',event);")


     
    End Sub
0
 
LVL 9

Expert Comment

by:valkyrie_nc
ID: 17073840
Beautiful!  
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 17356288
PAQ / No Refund
ee ai construct, community support moderator
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question