Iron Speed Technical Forums
Sign up Latest Topics
 
 
 


Reply
  Author   Comment  
ADW

Registered:
Posts: 106
Reply with quote  #1 
In several pages in my currrent application I call a couple of unctions to update some read-only fields on the page when a value is entered into other editable fields.  ATM I have replicated the function code in Section 1 of the PageName.Controls.vb code file for each of the pages concerned.  Rather than having to maintain these functions in several pages, I'd rather make them global functions that can be called from any page.
So my questions are:
1. Where can I put these functions so that they can be called from any page?
2. What is the correct syntax for calling global functions from within the page code?
3. How do I reference back to fields on the page that the function needs to update?

Any help on this or pointers to an example would be much appreciated.

ADW
echo4sos

Avatar / Picture

MVP Developer
Registered:
Posts: 172
Reply with quote  #2 
This should cover all of your questions. You may want to look at the Add New Record and/or Update Record CCW's. These create reusable subroutines that can be called from any page in your application. It would probably be best to follow the Add New Record CCW, as this creates the Shared Public Sub in the Business Layer of your application. 

So let's say that you created the AddRecord subroutine to a page you created called Orders.aspx based on a table called Orders. The sub would be created in ..\App_Code\Business Layer\OrdersRecord.vb(cs). Now if you were on a page called EditProducts.aspx, then from your EditProducts.aspx.vb(cs) code-behind page you can call the AddRecord sub like this: OrdersRecord.AddRecord()

This can be completely customizable and is not limited to subroutines. You can create custom Functions, as well as pass parameters to your Subs or Functions.

Hope this helps.

__________________
-Bill
dingjing

Avatar / Picture

MVP Developer
Registered:
Posts: 256
Reply with quote  #3 
Add your functions into BaseApplicationPage.cs (vb), or in a stand-alone file as extension methods.

In order to access controls on the actual pages, you can use FindControlRecursively("ControlName") method if the controls have the same names across pages. Otherwise, you can define a virtual property or method in BaseApplicationPage. Then implement it in actual pages.
timt

Avatar / Picture

Iron Speed MVP
Registered:
Posts: 556
Reply with quote  #4 
Hi

To add whats been said:

To allow a called function change the calling function fields on a page, you have to pass across the Page as a variable (or each of the fields that need changing as required.)

ie

if called from your page to a library pass this (Me) as the page instance.

MyNamespace.MyFunctionName(this,param1, param1.....)

HTH

__________________
Tim Titchmarsh Iron Speed Consultant MVP
London UK based
+44 (0)1621 835002
http://www.lightspeeditsolutions.co.uk/dotnetarchitect
timt@dotnetarchitect.co.uk
timt@lsits.co.uk
ADW

Registered:
Posts: 106
Reply with quote  #5 
Thanks for all the helpful responses, I've got it working nicely.

I added the Public subroutine to the end of the BaseApplicationPage.vb code file, just before the End Class statement and added a parameter for the current page:
    Public Sub MySubroutine(ByVal CurPage As Page)

I then called the subroutine from the appropriate places in the page code, mostly from the TextChanged() event for the editable fields:
    Me.Page.
MySubroutine(Me.Page)

In order to update the values of controls on the pages from the global subroutine, I had to use the following syntax:
    CType(FindControlRecursively(CurPage, "Control_ID"), System.Web.UI.WebControls.TextBox).Text = "NewValue"

I couldn't get the FindControlRecursively() function on it's own to compile without errors until I wrapped it in the CType() function.

Can anyone see any problems with this or suggest a better way of doing it.

Thanks,
ADW
dingjing

Avatar / Picture

MVP Developer
Registered:
Posts: 256
Reply with quote  #6 
You define a method in the page class, and pass the page reference to itself. It won't hurt anything, just completely unnecessary.
ADW

Registered:
Posts: 106
Reply with quote  #7 
Hi Dingjing,
Sorry, I don't follow what you mean.  I'm not all that clued up on Page Classes, Methods, etc. yet as this is my first .NET development project.  Would you mind explaining in a bit more detail please.

Thanks,
ADW

Previous Topic | Next Topic
Print
Reply

Quick Navigation:

Easily create a Forum Website with Website Toolbox.

Download Iron Speed Designer

Terms of Service Privacy Statement