Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 10th, 2006, 10:27 AM
Registered User
 
Join Date: Feb 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Passing Public Variables in Code-Behind

Please help, i'm a bit of a newcomer to .net and i'm having problems passing my variables around in my code-behind pages.

Can anyone tell me why my variable 'strFullNameLCase' is not carrying through to my final sub 'Button2_Click'?

I thought that 'strFullNameLCase' would be available to all subs/functions thereafter.


Partial Class StaffDetails

Inherits System.Web.UI.Page

         Public strFullNameLCase As String

         Public Function funDisplayPhotoImage(ByVal varEmailAddress As String)

                    strFullNameLCase = varEmailAddress

                    MsgBox(strFullNameLCase) 'works!

         End Function

         Public Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)

                    MsgBox(strFullNameLCase) 'doesnt work!

         End Sub

End Class


Thanks in advance
 
Old February 10th, 2006, 02:52 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

You need to assign a value to the string before you call the msgbox(). Otherwise you will not get a messagebox.

Jim

 
Old February 10th, 2006, 03:41 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

The whole idea of MsgBox is conceptually wrong in a Web Application. You may get the box, but you'll get it on the server. You may not notice it when you develop on your webserver, but your clients will never see the message box as it runs on your server.

Here's what happens:

1. The page loads, HTML is sent to the clent
2. You click Button2.
3. The page *posts back* to the server where the message box is displayed.

Not sure when you call funDisplayPhotoImage, but if it's called from a button too, the same process is repeated. The page posts back, assigns the variable a value and shows a *server side* message box. Then you click the second button, and the page posts to the server again. The Page instance (StaffDetails) is recreated again, and the value if strFullNameLCase doesn't exist.

You seem to be missing some fundamental knowledge in ASP.NET. You may want to read a couple of books about .NET that show you how the page architecture with post backs works....

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old February 10th, 2006, 05:49 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Imar is correct for the 1.0 and 1.1 versions of the framework. In 2.0 the msgbox actually does work. Just assign a value to the string first.

 
Old February 10th, 2006, 07:47 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I never said it doesn't work. I said it runs at the server. So, if you have 1 web server and 100 clients, none of the clients will see the message box; it's displayed at the server.

So, it works, but it's useless unless you want to click away everyone's messages boxes.

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old February 11th, 2006, 12:57 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Imar, I guess I was seeing the msgbox because I am running my app localy which is the server. I guess if I create a project on another server, and run it from my pc i will not see it. Is this what you are saying? Thanks.


 
Old February 11th, 2006, 04:58 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Exactly.

MsgBox runs in the context of the page executing at the *server*. It has nothing to do with the HTML being sent to the browser.

If you want a client side message box, that all users can see, you'll need to emit some JavaScript that uses the alert method to display a client side box. E.g.:

myLiteral.Text = "<script>alert('Hello World');</script>"

This code is sent to the browser where it will trigger a client side alert.

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old February 11th, 2006, 03:54 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

I have always done my pop ups wtih JavaScript. When I saw the msgbox funciton in 2.0 and it worked, I just assumed that got that fuctionalilty working server side. But then again how could that be? I just jumped to conclusions because I saw it on my pc (server).. Stuipd me.. Thanks again.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing Variables silve1999 Pro Java 1 June 15th, 2006 12:54 PM
Passing Variables CMOS Classic ASP Basics 2 January 22nd, 2006 07:38 PM
Modules and public variables EricJ Pro VB.NET 2002/2003 3 July 1st, 2005 08:57 AM
How to reference Public Variable in Code Gino Liu Access VBA 2 June 18th, 2003 01:42 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.