|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
App_Code class call funcrion in aspx.cs file?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) 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 > > Hello bruce,
> .net 101, your pages implement an interface defined in appcode. then I admit - You are persistant - I see you giving this canned response for > the handler can be cast the handler to the interface. > 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 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 > > Hello Muj,
Show quoteHide quote > If I understand your issue correctly, then the following should work: That doesn't 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 >> 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) 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 > >
Other interesting topics
autoincrement vs uniqueidentifier (easy)
Is Delegation Necessary? session state time out Using if on ASPX-side instead of in code behind Urgent - Issues with using Office 2003 PIA on Windows Server 2008 Data binding etc ASPNET or Network Service account on Vista Home Premium Using Access mdb file with Web Site Administration Tool Trap Data Tier Errors? Going to a link but need to log in. |
|||||||||||||||||||||||