Solved

Display data in datalist

Posted on 2006-07-10
10
375 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

912 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