move code behind file to module

G

Guoqi Zheng

Dear sir,

If I need to use a function on xxx.aspx page, e.g. on a repeater control, I
have to define this function inside xxx.aspx.vb file, if I put it in the
module class, it will not work so I have to copy the same function into all
aspx page which may need it.

Is it possible that I define this function something in the application, I
can use it on every .aspx page??

--
Kind regards

Guoqi Zheng
guoqi AT meetholland dot com
Http://www.meetholland.com
 
K

Karl

Guoqi:
Yes, the function doesn't have to reside in the codebehind file in order to
be usedin your aspx page.

namespace misc{
public sealed class Utility{
private Utility(){}
public static string FormatDate(object date) {
if (date == DBNull.Value){
return "n/a";
}
try{
return ((DateTime)date).ToShortDateString();
}catch{
return "n/a";
}
}
}
}


can be used from your aspx page, such as:

<asp:Repeater id="repeater" Runat="server">
<ItemTemplate>
<%# misc.Utility.FormatDate(DataBinder.Eval(Container.DataItem,
"Ordered"))%>
</ItemTemplate>
</asp:Repeater


or you can even put:

<%@ Import namespace="misc" %> at the top of the page and then do:

<asp:Repeater id="repeater" Runat="server">
<ItemTemplate>
<%# Utility.FormatDate(DataBinder.Eval(Container.DataItem,
"Ordered"))%>
</ItemTemplate>
</asp:Repeater>


Karl
 
B

bruce barker

the best approach for this is to create a base codebehind page, and have all
you other codebehinds inherit from this page.


the base page (myBasePage.cs):

public class MyBasePage : System.Web.UI.Page
{
// any common functions in here
}

on your code behind page , change the inheritance to MyBasePage:

public class MyCodeBehindPage : MyBasePage
{
}

-- bruce (sqlwork.com)
 
K

Karl Seguin

It would be helpful if you told us what the error is and perhaps provided
sample code.
 
K

Kevin Spencer

I believe your confusion stems from not thinking in Object-oriented terms.
At run-time, there are no "files" involved in the application process. Files
are compiled into classes, and classes are what you need to think about, not
files. For example, you can define multiple classes in a single file. So,
obviously, the file is irrelevant.

If you need functionality to be available to multiple Page classes, you
define that functionality in a class that is separate from your Page class
code. Then you use that class in your Pages.

This is not something you don't already know how to do. Every time you use a
..Net CLR class, function, or namespace, you are referring to classes that
exist outside your Page class. They exist in DLLs that are stored in the
Global Assembly Cache. When you create a file that defines a class,
regardless of whether you compile it into a DLL or not, you are creating the
same thing.

OOP is all about abstraction. Think of the abstract entities, the classes,
rather than the files and code that define them, and you'll be a lot better
off.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living
 

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,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top