How to trigger on-click of hyperlink inside dataList

10e5x

Member
Joined
Jun 29, 2012
Messages
14
Programming Experience
Beginner
i realise there are no on-click event of hyperlink in datalist. Anyone can enlighten me how to go about it. I would like to store information on the selected datalist item in session when hyperlinked is clicked. THanks
 
You can use OnItemDataBound event for this. Example:

<asp:DataList ID="dlTournamentMenu" runat="server" OnItemDataBound="Item_Created" RepeatColumns="4" CellSpacing="5" RepeatLayout="Table" SeparatorStyle-Width="10" CellPadding="4" BorderWidth="0">
<ItemTemplate>
<div class="uc4TournamentLinks" >
<asp:HyperLink ID="hylMenuItem" runat="server" NavigateUrl='<%# Eval("PageInternalLinkURL") %>' ToolTip='<%# Eval("PageName") %>' Target='<%# Eval("PageWindow") %>' BorderWidth="0px" Font-Underline="False" >
<asp:Label ID="lblTourTitle" CssClass="uc4TournamentLinksColor" runat="server" Text='<%# Eval("PageName") %>'></asp:Label>
</asp:HyperLink>
</div>
</ItemTemplate>
</asp:DataList>
protected void Item_Created(Object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
// Retrieve the Hyperlink control in the current DataListItem. HyperLink Link = (HyperLink)e.Item.FindControl("hylMenuItem"); string page = ((DataRowView)e.Item.DataItem).Row.ItemArray[1].ToString(); string GATrakking = "_gaq.push(['_trackEvent', 'Downloads', 'PDF', '"+ page.Trim() +"']);"; Link.Attributes.Add("onClick", GATrakking);
}
}
 
Back
Top