Calling a function in aspx page from a common class?

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
 
M

Martin Honnen

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())%>
 
R

Registered User

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

softwareakash

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top