Unable to set class variables in Page_Load

D

David Ibc

Hi,

I am having trouble setting class and session variables in the Page_Load
function in C#. No matter what I set the variables to in Page_Load they
are null when I try to access them in other code on the form.

- I have done this before in VB.Net and it has worked.
- I tried making the variables public (though they shouldn't need to be)
and got the same result
- If I move the same code into an on click event of a button on the web
form it works, so there is nothing in the code itself.

I am running VS 2003 on Windows 2000 Server. Below is some sample code.
If anyone can shed some light on this problem it would be much
appreciated. Also I tried setting breakpoints in Page_Load but the
debugger skips over it. Does anyone know how to get the debugger to step
through the Page_Load function?

private string strConn;
private MyObject obj;

private void Page_Load(object sender, System.EventArgs e)
{
strConn = ConfigurationSettings.AppSettings["ConnectionStr"];
obj = new MyObject(strConn);
Application["ConnectionString"] = strConn;
}

//obj will be undefined and strConn will be null
 
O

Olorin

David said:
Also I tried setting breakpoints in Page_Load but the
debugger skips over it. Does anyone know how to get the debugger to step
through the Page_Load function?

Actually this makes me think there is a larger problem here.
The sample code you posted seems ok to me.
The fact that the debugger doesn't get to the Page_Load method, on the
other end, is odd.

Is the page load in a codebehind of a page in the project that you
start in debugging mode?

If so, the debugger should get there.

Are you by chance using nUnit, or some other 3rd party library? I ask
because about a year ago I hit a similar problem when using a 3rd paty
tool... for some reason, as we added the references to it and tried to
use it, we lost the ability to debug through our code. I think we
simply had to close Visual Studio (all instances of it if you have
multiple ones open) and re-open it to make it work again....


Also,
since your Page_Load doesn't call any other method, I wonder if we are
slipping on some scoping issue.

Try

public class MyCodeBehind
{
private string str = "";

public void Page_Load(object sender, System.EventArgs e)
{
str = "Hello";
Test();
}
private void Test()
{
string s2 = str; //<-- check via debugger if str is undef here !
}

}

Finally, is this Page_Load method correctly wired to be the event
handler for the PageLoad event of the page that this codebehind class
is linked to?

HTH,
F.O.R.
 
D

David Ibc

Finally, is this Page_Load method correctly wired to be
Ding. ding. ding.

The InitializeComponent function was missing the following line:

this.Load += new System.EventHandler(this.Page_Load);

I think I had moved an old page out of the way and renamed a new page,
so maybe this got lost somewhere in the process. Also, I am now able to
step through Page_Load with the debugger.

Thanks for the advice. Much, much appreciated.
 

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