App_Code class call funcrion in aspx.cs file?

  • Thread starter Thread starter Karl Mitschke
  • Start date Start date
K

Karl Mitschke

I have a CommonFunctions.cs file in App_Code, which has several classes.

I can call the functions in these classes from my aspx pages with no problem.

Now, I'd like to streamline my code further.

Assume the following:

page1.aspx
page2.aspx
page3.aspx
page4.aspx

All pages have a code behind file.

In each cs file are three functions:

fnCreate_Object
fnDelete_Object
fnChangeAddress


In all cases, the contents of the functions differ based on the object type.

Now, in CommonFunctions, I validate various stuff, and assuming everithing
passes, I have a switch statememt to see what action is requested.

Once I know the action, how can I call the function on the referring page?

Using Page page = (Page)HttpContext.Current.Handler; I can access controls
on the page.

How can I access the functions on the page?

I suppose I could return a bool indicating the status of my validations,
and have page run the switch statement to determine the next steps, but I
have the feeling I should be able to call the functions directly?

Thanks

Karl
 
..net 101, your pages implement an interface defined in appcode. then the
handler can be cast the handler to the interface.

-- bruce (sqlwork.com)
 
Hello bruce,
.net 101, your pages implement an interface defined in appcode. then
the handler can be cast the handler to the interface.

I admit - You are persistant - I see you giving this canned response for
years.

Now, can you add some detail?

Assumong I have this, how can I access page1's fnCreate_Object?

public partial class Page1 : System.Web.UI.Page
{
CommonFunctions.fnGetData();
void fnCreate_Object()
{
Response.Write("Hello"
}
}

public class CommonFunctions
{
public static void fnGetData()

{
//Access Page1.fnCreate_Object
}
}

Karl
 
If I understand your issue correctly, then the following should work:

public partial class Page1: Page
{
internal SomePageMethod()
{
}
}

Form outside the Page1 class, you can call this function by casting it to
the proper page derived type first. So, in CommonFunctions.cs:

public class ComonFunctions
{
public static SomeFunc()
{
Page1 page = (Page1)HttpContext.Current.Handler;
page.SomePageMethod();
}
}


Thanks,
Muj Beg
 
Hello Muj,
If I understand your issue correctly, then the following should work:

public partial class Page1: Page
{
internal SomePageMethod()
{
}
}
Form outside the Page1 class, you can call this function by casting it
to the proper page derived type first. So, in CommonFunctions.cs:

public class ComonFunctions
{
public static SomeFunc()
{
Page1 page = (Page1)HttpContext.Current.Handler;
page.SomePageMethod();
}
}
Thanks,
Muj Beg

That doesn't work:

The type or namespace name 'Page1' could not be found (are you missing a
using directive or an assembly reference?)

Karl
 
the main design decision of .net (over objective c, ruby, python, etc)
is requiring interfaces for polymorphic method calls. this is why you
should read a chapter on interfaces.


// define interface in appcode

public interface IMyPageFunctions
{
void fnCreate_Object();
}

// class implements interface

public partial class Page1 : System.Web.UI.Page, IMyPageFunctions
{
CommonFunctions.fnGetData();
void fnCreate_Object()
{
Response.Write("Hello"
}
}


public class CommonFunctions
{

public static void fnGetData()

{
// cast handler (page) to interface and call
var handler = HttpContext.Current.Handler as IMyPageFunctions;
if (handler != null)
handler.fnCreate_Object();
}
}

-- bruce (sqlwork.com)
 
Hello bruce,
the main design decision of .net (over objective c, ruby, python, etc)
is requiring interfaces for polymorphic method calls. this is why you
should read a chapter on interfaces.

// define interface in appcode

public interface IMyPageFunctions
{
void fnCreate_Object();
}
// class implements interface

public partial class Page1 : System.Web.UI.Page, IMyPageFunctions
{
CommonFunctions.fnGetData();
void fnCreate_Object()
{
Response.Write("Hello"
}
}
public class CommonFunctions
{
public static void fnGetData()

{
// cast handler (page) to interface and call
var handler = HttpContext.Current.Handler as IMyPageFunctions;
if (handler != null)
handler.fnCreate_Object();
}
}

Cool...

Thanks

I should have mentioned I'm on .net 2.x, so I get "The type or namespace
name 'var' could not be found"

Karl
 
Hello bruce,
Hello bruce,

Cool...

Thanks

I should have mentioned I'm on .net 2.x, so I get "The type or
namespace name 'var' could not be found"

Karl

I think I sorted it out:

IMyPageFunctions handler = HttpContext.Current.Handler as IMyPageFunctions;

Now to attemt to impliment it :)

Thanks again
Karl
 
Hello bruce,

Once again, THANKS!

By doing this, I save approximately 2000 lines of redundant code.

Karl
the main design decision of .net (over objective c, ruby, python, etc)
is requiring interfaces for polymorphic method calls. this is why you
should read a chapter on interfaces.

// define interface in appcode

public interface IMyPageFunctions
{
void fnCreate_Object();
}
// class implements interface

public partial class Page1 : System.Web.UI.Page, IMyPageFunctions
{
CommonFunctions.fnGetData();
void fnCreate_Object()
{
Response.Write("Hello"
}
}
public class CommonFunctions
{
public static void fnGetData()

{
// cast handler (page) to interface and call
var handler = HttpContext.Current.Handler as IMyPageFunctions;
if (handler != null)
handler.fnCreate_Object();
}
}

Kar
 
Hi Karl,
I have the same problem to solve as described by you here. I am coding this
in vb. Can you please send me the exact implementation that worked for you???
If needed, I can send you the exact explaination about what I need to do.

Thanks!!!

Harry.
 
I am sorry, I mean to say vb.net.

Harry said:
Hi Karl,
I have the same problem to solve as described by you here. I am coding this
in vb. Can you please send me the exact implementation that worked for you???
If needed, I can send you the exact explaination about what I need to do.

Thanks!!!

Harry.
 
Hello Harry,

I'm the wrong one to ask about this :)

Perhaps ask in one of the VB newsgroups.
Hi Karl, I have the same problem to solve as described by you here. I
am coding this in vb. Can you please send me the exact implementation
that worked for you??? If needed, I can send you the exact
explaination about what I need to do.

Thanks!!!

Harry.

Kar
 
Back
Top