Procedure not accessible when private in webform

T

Tim Zych

If I declare a procedure in a webform as Public and attach it to a button in
the same webform, it runs fine. If I change it to Private Sub and try to
click it i get the error:

'codelib.editcode.Private Sub SaveRecord(sender As Object, e As
System.EventArgs)' is not accessible in this context because it is
'Private'.

What am I doing wrong? Everything is in one webform.

Private worked in my notepad version of the project. Why is it not working
in my vsnet version?

Thanks
 
K

Kevin Spencer

If you're using a CodeBehind class, the Page Template inherits it, which
means that Private CodeBehind members will not be accessible to the Page
Template. Make it Protected instead.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
S

Scott Mitchell [MVP]

If I declare a procedure in a webform as Public and attach it to a button in
the same webform, it runs fine. If I change it to Private Sub and try to
click it i get the error:

'codelib.editcode.Private Sub SaveRecord(sender As Object, e As
System.EventArgs)' is not accessible in this context because it is
'Private'.

What am I doing wrong? Everything is in one webform.

When the ASP.NET Web page is visited, it is turned into a class that is
*DERIVED* from the code-behind class (which is, in turn, inherited from
the System.Web.UI.Page class). So, the code-behind class becomes a base
class for the actual class that is executed. Inheritence, as you may
know, keeps private members private, but protected and public members
inherit through. So your event handlers need to be protected or public
when using the code-behind model.
Private worked in my notepad version of the project. Why is it not working
in my vsnet version?

In the model where you have the code in a server-side script block, the
class autogenerated is derived directly from the Page class, with the
methods in the server-side script block embedded directly in the
autogenerated class. So they can be private.

For more information, see:

The ASP.NET Page Object Model
http://msdn.microsoft.com/library/d.../en-us/dnaspp/html/aspnet-pageobjectmodel.asp

Happy Programming!

--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top