Calling Code behind method in Kentico Transformation

This article describes about the calling code behind method from the transformation. We usually make use of Transformation to display the data of a document type. But, sometimes we need call code behind method to perform some logic based on clicks.

We have faced similar situation where we have to count the number clicks on each news item as per the requirement. We are using the transformation to display the list of news.

Following code is used to achieve the requirement:

Transformation [ASCX] of displaying news list:

Calling code behind method on button click 

<div class="row-bor">

<div class="news-row padlr10">

  <div class="news-img"><%#GetImage("NewsTeaser") %></div>

  <div class="news-text w580">

    <span class="f13">

    <b>

      <asp:LinkButton EnableViewState="false" id="lnkTitle" runat="server" 

onCommand="Popularity" CommandName='<%#GetDocumentUrl() %>' CommandArgument='<%#Eval("NewsID")%>'> <%#Eval("NewsTitle")%> </asp:LinkButton>      

   </b>

      </span>

      <p><%# CustomTrimText(Eval("NewsSummary"),250) %></p>

       <div class="f11"><a href="<%#GetDocumentUrl() %>"><b>More Information</b></a>

       <div class="clear"></div>

  </div>

    </div>

  <div class="clear"></div>

  </div>

</div>

SCRIPT

Calling Code behind method using Popularity function (To be written under the Transformation)

<script runat="server">

protected void Popularity(object sender,CommandEventArgs e)       

{      

  NewsHandler.NewsPopular(e.CommandArgument.ToString(),e.CommandName.ToString());                

}

 </script>

Following is the class with method to count the clicks ( To be added under the App_Code ):

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using CMS.PortalControls;

using CMS.GlobalHelper;

using CMS.CMSHelper;

using CMS.Controls;

using CMS.PortalEngine;

using CMS.ExtendedControls;

using CMS.SettingsProvider;

using CMS.SiteProvider;

using System.Data;

/// <summary>

/// Summary description for NewsHandler

/// </summary>

public static class NewsHandler

{

public static void NewsPopular(string newsId,string url)

{

       TA_News objNews = new TA_News();

         int status=objNews.UpdateNewsHits(Convert.ToInt32(newsId));

         HttpContext.Current.Response.Redirect(url);

}

}

- My ASP.NET Application
We use cookies to provide the best possible browsing experience to you. By continuing to use our website, you agree to our Cookie Policy