Learning
Learning

Reputation: 20001

Add attribute to HyperLink Control inside DataList control

I am trying to add GA Tracking code to Hyperlink which is inside Datalist Control, With the below code i am able to add but then all the GA Tracking event will show all pdf file will the same name. Below is the code

<asp:DataList ID="dlTournamentMenu" runat="server" 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") %>' onclick="_gaq.push(['_trackEvent', 'Downloads', 'PDF', 'PDF File Downloaded']);" 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> 

When i try to add add code as below

onclick="_gaq.push(['_trackEvent', 'Downloads', 'PDF', '<%# Eval("PageWindow") %>']);"

it generates error as i cant bind Eval to this property.

How can i bind this grammatically like we do it for Repeater control as example below

    protected void gvUsers_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        //LOCK USER
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton lbLockUser = e.Row.FindControl("lnkBtnLockUser") as LinkButton;
            if (lbLockUser != null)
                ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(lbLockUser);

            lbLockUser.Attributes.Add("onclick", "javascript:return " +
            "confirm('Are you sure you want to lock this User ')");
        }

} 

Upvotes: 1

Views: 4080

Answers (1)

Learning
Learning

Reputation: 20001

UPDATED: RESOLVED solution i did it using OnItemDataBound event complete working code below

<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);
    }

    }

Upvotes: 1

Related Questions