0

I have an ASPX page that calls a user control (ASCX) to render a form. The control includes code-behind that fires when a certain button is clicked, like this:

Protected Sub ClickControlButton(ByVal sender as Object, ByVal e as System.EventArgs)
    ' run some code
    ' need to put something here, which I'll explain below
    Response.Redirect(Request.RawUrl)
End Sub

(For example purposes, let's say this control is called ChildControl.ascx. Note: the Response.Redirect call ends up going back to the parent page, so when it fires, it effectively reloads the page.) ChildControl.ascx is embedded within another form that I'll call ParentPage.aspx.

I need this sub to call another sub in the parent page (let's call it ParentPage.aspx) code-behind an instance inherited by ParentPage.aspx (see my edit explanation below), something like this:

Protected Sub SomeFunction()
    'run some more code
End Sub

I need this called before the redirect happens. So, effectively, when all is said and done, I need my control code to look something like this:

Protected Sub ClickControlButton(ByVal sender as Object, ByVal e as System.EventArgs)
    ' run some code
    SomeFunction()
    Response.Redirect(Request.RawUrl)
End Sub

However, the control does not recognize SomeFunction(), probably because it is a Protected Sub, not Public, (EDIT) and it is an instance class. I'm trying to avoid changing it to a Public Sub.

How would I go about handling this?

Thanks in advance . . .

EDIT: A colleague, who knows more about this system than I do, pointed out that the method I'm trying to access is not in the ParentPage.aspx, as I'd mentioned earlier, but is actually an instance method stored within a separate VB file (it is, however, accessible via Inherit from ParentPage.aspx -- apologies for the confusion in my original question). So reflection would not work, in this case.

Ray K.
  • 2,431
  • 3
  • 25
  • 39
  • 1
    You can use reflection to invoke private method - duplicate of [How do I use reflection to invoke a private method?](http://stackoverflow.com/questions/135443/how-do-i-use-reflection-to-invoke-a-private-method) – T.S. Dec 03 '15 at 19:36
  • Reflection is a new concept to me. Reading suggestions? – Ray K. Dec 03 '15 at 19:49
  • Just look at that question and it will give you a good start specific to your problem. BTW, @user1050979 asked legit question - what is data type of `ParentPage`? – T.S. Dec 03 '15 at 20:22
  • Yeah, taking some time to digest it. It's taking me a little extra time, since that question is in C#, and I'm in VB. – Ray K. Dec 03 '15 at 20:48
  • In case of reflection, it is same really - you just call methods. But you need to get used to C# even though you work in VB because most info you find is in c#. – T.S. Dec 03 '15 at 20:57

2 Answers2

1

Well, first of all, make sure you the sub in the parent page is actually protected, using Object Browser (in the settings dropdown icon, click on Show Protected and make sure the function is protected). If not may be you are not casting the page object to the actual type of object, so the intelisense is not detecting the method while you have Option Strict On.

If its truly protected and not accessible, the only way is to use Reflection to invoke the sub.

Here is some reading material for System.Reflection

http://csharp.net-tutorials.com/reflection/introduction/

  • Good information (which is why I upvoted), but unfortunately, isn't helpful for resolving my issue (see my edits -- apologies for not clarifying sooner, but I'm learning a few things about this environment as well). – Ray K. Dec 04 '15 at 16:26
0

After consulting with my colleagues, I made the method into a Public Shared Sub.

Ugh!

Ray K.
  • 2,431
  • 3
  • 25
  • 39