Home All Groups Group Topic Archive Search About

App_Code class call funcrion in aspx.cs file?

Author
9 Jul 2009 5:18 PM
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

Author
9 Jul 2009 5:29 PM
bruce barker
..net 101, your pages implement an interface defined in appcode. then the
handler can be cast the handler to the interface.

-- bruce (sqlwork.com)

Karl Mitschke wrote:
Show quoteHide quote
> 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
>
>
Are all your drivers up to date? click for free checkup

Author
9 Jul 2009 7:18 PM
Karl Mitschke
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
Author
9 Jul 2009 10:34 PM
Muj Beg
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

Show quoteHide quote
"Karl Mitschke" <karlmitsc***@somestate.gov> wrote in message
news:d66cd4c2ecca8cbceaed599a3cd@msnews.microsoft.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
>
>
Author
9 Jul 2009 10:46 PM
Karl Mitschke
Hello Muj,

Show quoteHide quote
> 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
> "Karl Mitschke" <karlmitsc***@somestate.gov> wrote in message
> news:d66cd4c2ecca8cbceaed599a3cd@msnews.microsoft.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
>>

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
Author
9 Jul 2009 10:59 PM
bruce barker
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)

Karl Mitschke wrote:
Show quoteHide quote
> 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
>
>

Bookmark and Share