Calling a function in aspx page from a common class?

  • Thread starter Thread starter softwareakash
  • Start date Start date
S

softwareakash

Hi

I have a question relating to calling common functions in an aspx
page.

Here is the scenario :

I have various pages where I use repeaters to display databound data.
On all repeaters I have to format date to a specific type of string
( ie: 12hours ago, 2 days ago etc)

I have written a function for this Named : ToRelativeDateShort(string
dateTime)

I am calling this function inside the aspx page ( inside repeater )
like this

Posted : <%# ToRelativeDateShort(Eval("createddate").ToString())%>

And this is WORKING FINE, no problems in this the function, it is
being called and date is formatted as desired.

The problem is that I have to put the same function in all pages code
behind and since I have approx 50 pages
to have this functionality I have 50 instances of the same function in
all pages.

When I try to put this function in a common Procedures.cs and call it
like :

Posted : <%# Procedures.ToRelativeDateShort(Eval
("createddate").ToString())%>

It does not work, may be because the instance of the procedure class
is not created on this call. My present approach is working but its
inefficient to have 50 instances of the same function.. So I want to
ask what is the correct method to do this.. I am sure this scenario
should be very common ....





Thanks in Advance
Akash
 
softwareakash said:
Posted : <%# Procedures.ToRelativeDateShort(Eval
("createddate").ToString())%>

It does not work, may be because the instance of the procedure class
is not created on this call. My present approach is working but its
inefficient to have 50 instances of the same function.. So I want to
ask what is the correct method to do this.. I am sure this scenario
should be very common ....

Make the ToRelativeDateShort method a static (C#) respectively Shared
(VB) method of your Procedures class.

Or, as an alternative, create a new Procedures instance when needed e.g.
<%# new
Procedures().ToRelativeDateShort(Eval("createddate").ToString())%>
 
Hi

I have a question relating to calling common functions in an aspx
page.

Here is the scenario :

I have various pages where I use repeaters to display databound data.
On all repeaters I have to format date to a specific type of string
( ie: 12hours ago, 2 days ago etc)

I have written a function for this Named : ToRelativeDateShort(string
dateTime)

I am calling this function inside the aspx page ( inside repeater )
like this

Posted : <%# ToRelativeDateShort(Eval("createddate").ToString())%>

And this is WORKING FINE, no problems in this the function, it is
being called and date is formatted as desired.

The problem is that I have to put the same function in all pages code
behind and since I have approx 50 pages
to have this functionality I have 50 instances of the same function in
all pages.

When I try to put this function in a common Procedures.cs and call it
like :

Posted : <%# Procedures.ToRelativeDateShort(Eval
("createddate").ToString())%>

It does not work, may be because the instance of the procedure class
is not created on this call. My present approach is working but its
inefficient to have 50 instances of the same function.. So I want to
ask what is the correct method to do this.. I am sure this scenario
should be very common ....
Derive a type from System.Web.UI.Page and use that class as a
container for the function.

public class MyPage : System.Web.UI.Page
{
protected void MyMethod()
{
...
}
}

Add a new page to the project and change
public class WebForm1 : System.Web.UI.Page
to
public class WebForm1 : MyPage

Through inheritence every page which is derived from MyPage will be
able to access the MyMethod function.

regards
A.G.
 
Great .. it worked .. thanks a lot


I used <%# new
Procedures().ToRelativeDateShort(Eval("createddate").ToString())%>

However is there any performance issue with all these methods
suggested above? Which is the best to use?

Akash
 
Back
Top