Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net pass variable from code behind to .aspx

Tags:

asp.net

vb.net

I guess I'm missing something here, but I can't find a way to pass a simple variable from my code behind file to the .aspx page.

In code behind I have:

Dim test As String = "test"

and in my aspx page I try: <%=test %>

that gives me the following error: Error 2 'test' is not declared. It may be inaccessible due to its protection level

Am I forgetting something here?

like image 746
Jorre Avatar asked Sep 25 '10 14:09

Jorre


2 Answers

Declare test as a property (at the class level) instead of a local variable, then refer to it as you currently do in your markup (aspx).

VB.NET 10 (automatic properties):

Protected Property test As String = "Test" 

Pre-VB.NET 10 (no support for automatic properties)

Private _test As String
Protected Property Test As String
Get
     Return _test
End Get
Set(value As String)
     _test = value
End Set
End Property

With the property in place you should assign a value to it directly in your code-behind.

like image 135
Ahmad Mageed Avatar answered Nov 16 '22 19:11

Ahmad Mageed


Use the protected modifier.

Protected test As String = "test"
like image 28
Max Toro Avatar answered Nov 16 '22 19:11

Max Toro