asp.net - access page variables from non-code behind class modules

I have an asp.net aspx page with a large codebehind module.    I'd like to break the codebehind into class modules for easier navigation during maintenance.

 I've moved most of the functions that don't require access to page variables (i.e. HiddenFields, DropDownLists, etc) to separate class modules.  However, the codebehind module is still large and unwieldy.

Is there any way to create a class module, separate from the codebehind, that can reference those types of page variables?
LVL 2
codequestAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
It might be possible to split the file into partial files.  How large is the code module for the page?
0
codequestAuthor Commented:
The code behind is about 1600 lines of vb.net.  Lots of comments and blank lines for readability.  Breaking it into smaller modules would increase navigability/readability still more for easier maintenance.  

Ideally I would split it into the code behind and two other modules, along the lines of three major functional divisions.  Or even just two:  1100 and 500.   The 500 has this mass of DDL processing.
0
Bob LearnedCommented:
Without seeing the code, it would be difficult to suggest splitting techniques (not that I want to see 1600 lines of code!!).  The principal of single responsibility states that "every class should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by the class. All its services should be narrowly aligned with that responsibility."  When you say that you have 3 major functional divisions, that is a good start to good design.

Single Responsibility Principal
http://en.wikipedia.org/wiki/Single_responsibility_principle

Also, there are anti-patterns, like "big ball of mud" or "god object", that we should all keep in mind:

http://en.wikipedia.org/wiki/Anti-pattern
0
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Bob LearnedCommented:
There might be ways to simplify and refactor the code, that wouldn't require so many lines...
0
codequestAuthor Commented:
Ah, I may have not been clear in my initial question.

What I'm looking for is a way to reference and modify page variables, like the name and attributes of a DropDownList, from a class module that is NOT the code behind module.

Any thoughts on that?
0
Bob LearnedCommented:
You could pass a reference to the Page, and use Page.FindControl to get a reference to the controls on the page.  Or, if the number of controls is small, then you could also page a direct reference to each control.
0
codequestAuthor Commented:
Thanks for the response.   I'm not familiar with how to pass a reference to the Page.  How is that done?
0
Bob LearnedCommented:
You would need something like this example:

Dim helper As New HtmlHelper()
helper.ProcessButtonClick(ProcessButton, EmployeeGrid)

...

Public Class HtmlHelper

    Public Sub ProcessButtonClick(ByVal processButton As Button, ByVal employeeGrid As GridView)
        ' Do some processing here
    End Sub
End Class

Open in new window

0
codequestAuthor Commented:
Perhaps this is a better way to explain what I'm looking for (pardon the pseudocode):

1. page:
<%page "MyPage" codebehind = "MyPage">
   <body>
   < DropDownList  ID="MyDDL" + (other attributes)>
   </body>

2. code behind module
Partial Class MyPage inherits page
    Sub Load
         If not is postback then
             UTIL.DoStuffWithDDL
         end if
    End sub
End Class

3.  Class module
Class UTIL
    Public shared sub DoStuffWithDDL()
         Dim wrkMyPageReference  as ReferenceToPage = "MyPage"
         wrkMyPageReference.MyDDL.SomeAttribute = "SomeValue"
    End sub
End class

So the question is, is this possible, and if so, how to specify " Dim wrkMyPagAReference  as ReferenceToPage"?
0
Bob LearnedCommented:
Try something like this:

UTIL.DoStuffWithDDL(MyDDL)

...

Class Util
      Public Shared Sub DoStuffWithDDL(ByVal myDDL As DropDownList)
          MyDDL.SomeAttribute = "SomeValue"
      End Sub
End Class

Open in new window

0
Bob LearnedCommented:
Or page:

UTIL.DoStuffWithDDL(Page)

...

Class Util
      Public Shared Sub DoStuffWithDDL(ByVal page As Page, ByVal id As String)
           Dim ddl As DropDownList = TryCast(page.FindControl(id), DropDownList)
           ddl.SomeAttribute = "Some Value"
      End Sub
End Class

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
codequestAuthor Commented:
That works.  Boggle.  But, hey why not, they're all just objects.    And, once the TryCast is set up, then the code could be relatively "efficient" with respect to referencing the page elements.   So, big help, thanks!
0
Bob LearnedCommented:
If you are not used to TryCast, it won't raise an exception if the control ID is not a DropDownList, it will just be Nothing.  If you need an exception, then you would use DirectCast.
0
codequestAuthor Commented:
Good tip, thanks.
0
CamilliaCommented:
this was a good thread...i've been following it .
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.