Variable scope in ascx file

J

John Mason

Hi,

Could someone please help?! I am declaring a global variable in an ascx
file...

<%@ Control Language="VB" runat="server" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<%@Import namespace="System.Web.Mail"%>

<script language="VB" runat="server">
Dim strDriver as String
..
..

...then I assign this variable a value in a sub...

Sub edit_Clicked(sender as Object, e As DataGridCommandEventArgs)

strDriver = CType(E.Item.FindControl("lblDriverName"), Label).Text
..
..
.. in another sub, I am trying to retrieve the stored value... but it's
coming up blank...

Sub update_Clicked(ByVal sender as Object, ByVal e As
DataGridCommandEventArgs)

strText = "Driver name is " & strDriver & ".<br/>"


Probably something simple! I am new to ASP.NET. Much appreciated!
 
M

Marina

The page, and hence the user control in it, and hence all the variables in
the user controls - are all recreated on every request. This is because
HTTP is stateless, and so there is no other choice.

Because everything is reinitialized from scratch - your variable is empty
every time a new instance is created. So setting its value on one postback,
will not persist on another one.

You will need to find another way to store this information, such as
viewstate or session.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top