 |
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
|
|
|

February 10th, 2006, 10:27 AM
|
Registered User
|
|
Join Date: Feb 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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 
|

February 10th, 2006, 02:52 PM
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
You need to assign a value to the string before you call the msgbox(). Otherwise you will not get a messagebox.
Jim
|

February 10th, 2006, 03:41 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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.
|

February 10th, 2006, 05:49 PM
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
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.
|

February 10th, 2006, 07:47 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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.
|

February 11th, 2006, 12:57 AM
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
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.
|

February 11th, 2006, 04:58 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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.
|

February 11th, 2006, 03:54 PM
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
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.
|
|
 |