Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 10th, 2006, 10:27 AM
Registered User
 
Join Date: Feb 2006
Location: Spalding, Lincs, United Kingdom.
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
Reply With Quote
  #2 (permalink)  
Old February 10th, 2006, 02:52 PM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
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

Reply With Quote
  #3 (permalink)  
Old February 10th, 2006, 03:41 PM
Imar's Avatar
Wrox Author
Points: 72,045, Level: 100
Points: 72,045, Level: 100 Points: 72,045, Level: 100 Points: 72,045, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,083
Thanks: 80
Thanked 1,587 Times in 1,563 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.
Reply With Quote
  #4 (permalink)  
Old February 10th, 2006, 05:49 PM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
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.

Reply With Quote
  #5 (permalink)  
Old February 10th, 2006, 07:47 PM
Imar's Avatar
Wrox Author
Points: 72,045, Level: 100
Points: 72,045, Level: 100 Points: 72,045, Level: 100 Points: 72,045, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,083
Thanks: 80
Thanked 1,587 Times in 1,563 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.
Reply With Quote
  #6 (permalink)  
Old February 11th, 2006, 12:57 AM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
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.


Reply With Quote
  #7 (permalink)  
Old February 11th, 2006, 04:58 AM
Imar's Avatar
Wrox Author
Points: 72,045, Level: 100
Points: 72,045, Level: 100 Points: 72,045, Level: 100 Points: 72,045, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,083
Thanks: 80
Thanked 1,587 Times in 1,563 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.
Reply With Quote
  #8 (permalink)  
Old February 11th, 2006, 03:54 PM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
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.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 12:48 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.