1

我正在尝试将 GA 跟踪代码添加到 Datalist 控件内部的超链接中,使用以下代码我可以添加,但是所有 GA 跟踪事件将显示所有 pdf 文件将具有相同的名称。下面是代码

<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> 

当我尝试添加如下代码时

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

它会产生错误,因为我无法将 Eval 绑定到此属性。

我怎样才能像我们为Repeater控件做的那样在语法上绑定它,如下例所示

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

} 
4

1 回答 1

1

更新:解决的解决方案我使用OnItemDataBound下面的事件完整工作代码完成了它

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

    }
于 2012-11-27T06:10:09.900 回答