Ray K.
Ray K.

Reputation: 2511

Calling a protected sub instance method from a child control

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.

Upvotes: 0

Views: 1587

Answers (2)

Ray K.
Ray K.

Reputation: 2511

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

Ugh!

Upvotes: 0

sathish4000
sathish4000

Reputation: 13

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/

Upvotes: 1

Related Questions