accessing protected variables

M

Mark Hanson

I would like to access code behind .aspx.vb protected variables from
the aspx web page like this <%# _ProtectedVariable %>. The only way I
can get the value is if I do Me.DataBind on each postback. If I don't
call Me.DataBind then the protected variable is empty on the web page.
But I don't want to call DataBind which rebinds all my controls if
their DataSource is set. How can I make protected variables "stick"
without calling DataBind on the whole page? Viewstate has the same
issue, that Me.DataBind must be called in order to access the
viewstate value from the aspx web page.

In aspx form:
<%# _ProtectedVariable %>

In aspx.vb class:
Protected _ProtectedVariable as String

In aspx.vb Page_Load:
_ProtectedVariable = "Value"

In aspx.vb Page_PreRender:
'it works only if this line is not commented
'Me.DataBind
 
M

Marina

Huh? No idea what you are talking about.

You have to reset all local variables when the page is recreated - because
since the page is recreated, so is everything on it. And if you don't set
your variables to a value every time, they will remain blank. This is not a
connected environment - not a winforms app. You get a brand new object
every time there is a request - not the one whose variables you set last
time, that one is long gone.

What does this have to do with the variables being protected?
 
K

Kevin Spencer

<%# %> is a DataBinding expression. If you use a DataBinding expression, you
must call DataBind to bind the data.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living
 
B

bruce barker

that is expected behavior. the databinder is what evalutes the binding
string expression <%# bindingExpression %>, and replaces it with its actual
value.

-- bruce (sqlwork.com)
 
M

Mark Hanson

Bruce and Kevin,

Thanks for your reply. Do you know of a way to bind variables without
binding the whole page? I prefer not to databind other controls on
postback but I do want to update certain variables and access them
from the aspx page. Controls have a DataBind method and I would like
to databind specific variables ideally.

Mark
MCP asp.net
 
K

Kevin Spencer

You can call DataBind on the Control which hosts the DataBinding expression.
If that is the Page, that is the Control that you need to call DataBind on.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living
 

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

Top