Solved

Display data in datalist

Posted on 2006-07-10
10
374 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
 

Author Comment

by:duynnh
ID: 17073746
Hi valkyrie_nc!
I don't use HyperLink, i just use <img> tag
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now