2

I would like to have an aspx page that contains something like....

<form id="form1" runas=server >
    Hello <%= Me.UserName() %>
</form>

and a code-behind something like...

Public Class Somepage
    inherits SomeOtherPage
    Private Readonly Property UserName() as String
    Get
        return "Rory"
    End Get
    End Property
End Class

I have tried this code but the aspx errors claiming that UserName is not declared.

What is the proper way to do this?

1 Answer 1

3

Mark the property as Protected, not Private.

Protected Readonly Property UserName() as String    
     Get        
          return "Rory"    
     End Get    
End Property
3
  • I forgot that aspx inherits from the Declared class rather than being partial with it... Man I feel dumb now :) Nov 13, 2008 at 14:27
  • The other way to do this, and in my opinion the preferred method that preserves encapsulation, is to use a label control and explicity set the text property of the label.
    – TGnat
    Nov 13, 2008 at 14:36
  • Agreed. My example was vastly oversimplified. In reality I'm populating the value attribute of a number of htmlinput controls whose 'name' attributes must be fixed and therefore I cannot risk the name mangling which might result from using equivalent ASPx controls :) Nov 13, 2008 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.