Solved

Display data in datalist

Posted on 2006-07-10
10
377 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

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…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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