Calling a method in different aspx.vb code page

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I've got a VB .NET Web application that has several frames (for discussion
sake, let's call them Form1.aspx and Form2.aspx). I want to be able to call
a code module in Form2.aspx.vb from code module in Form1.aspx.vb. How do I
format this statement?
I'm not interested in calling client-side script on Form2.aspx from
Form1.aspx.vb - I want to fire off server-side module in Form2.aspx.vb
 
You just instantiate the class and call the method, as you would with any
other sort of class that wasn't a web page. The method must not be declared
private - public or friend will do. Let's say your Form2.aspx.vb file has a
public method like:

public function SayHello() As String
return "Hello"
end function

Then in form1.aspx.vb, you'd do something like:

Dim otherForm As Form2 = New Form2()
Dim result As String = otherForm.SayHello()

It appears from my testing that Form2's Page_Load method does not get called
during this process.

Another approach, perhaps more desirable from an architectural standpoint,
would be to factor the code out of form2 into an external class, which would
then be used by both form1 and form2. You could take the code and put it
into a Shared member of a utility class, and then call the method from both
forms. If you can write the code such that it does not require access to the
web context, then it's quite simple to do. If it needs to get to the Request
or Response objects, for instance, then you need to take a little further
action, referring to, for instance, HttpContext.Current.Request. instead of
just Request.

Tom Dacon
Dacon Software Consulting
 
Tom - thanks for the quick reply. The only problem I see with your
suggestion is that it will create a new occurance of Form2. I don't want to
do this, because the function on Form2 will be manipulating the contents of a
listbox on the page. I need to get the original page (the one that's
currently being display). Any ideas?
 
So let me see if I understand: the Form1 page is either being loaded
(page_load) or responding to a server-side event, and in form1's code-behind
code you want to call a method in the code-behind code of a different page
(form2). That code now does something to a control on the form2 page (which
isn't currently being displayed). But you want the code on form2 to do
something to a control on the form1 page, when you call it from form1, but
do something to a control on the form2 page when it's called from form2. Am
I right here?

If you change that function so that it receives as an argument the control
it's supposed to work on, and then change the code in form2 so that it
passes the control to the function, then you could also call the function
from form1, passing to it the control that resides on form1. Once this
interface change was made, you could instantiate form2 and call the method
as I described in the first part of my earlier post.

However, if the code applies equally to form1 and form2, then neither form
can be said to 'own' it, and it makes sense to move it out to a separate
class as I described in the second part of my earlier post. Either a member
of a module, or a Shared member of a class.

Tom Dacon
Dacon Software Consulting
 
Back
Top