-
Nov 16th, 2009, 09:31 PM
#1
Thread Starter
PowerPoster
Popping up a messagebox using ASP.Net???
I know in VB.Net I can use "messagebox.show". How do I popup a message box in ASP.Net?
Thanks,
-
Nov 16th, 2009, 10:42 PM
#2
Re: Popping up a messagebox using ASP.Net???
You have to use javascript to show an alert box.
HTML Code:
<script type="text/javascript">alert('test');</script>
If you want to do it from the ASP.Net code, you would have to either add a client script to the page header containing the alert() or hack it with Response.Write("<script>..."); to write the javascript alert() to the page output.
-
Nov 17th, 2009, 03:36 AM
#3
Re: Popping up a messagebox using ASP.Net???
Hey,
I would highly recommend that you don't use Response.Write for this!!
Do things properly!!
Another alternative would be the Modal Popup Extender from the AJAX Control Toolkit:
http://www.asp.net/AJAX/AjaxControlT...odalPopup.aspx
Or, something like the Confirm Button:
http://www.asp.net/AJAX/AjaxControlT...irmButton.aspx
Gary
-
Nov 17th, 2009, 09:50 AM
#4
Hyperactive Member
Re: Popping up a messagebox using ASP.Net???
-
Nov 17th, 2009, 11:02 AM
#5
Re: Popping up a messagebox using ASP.Net???
Hey,
MsgBox, or rather MessageBox, is a Windows Form Control, not an ASP.Net Component. You will find alternatives of this for ASP.Net Development, but at their heart, they will implement some form of JavaScript alert.
Gary
-
Nov 17th, 2009, 01:54 PM
#6
Hyperactive Member
Re: Popping up a messagebox using ASP.Net???
But Msgbox("Works with me in ASP.NET")
-
Nov 17th, 2009, 02:21 PM
#7
Thread Starter
PowerPoster
Re: Popping up a messagebox using ASP.Net???
Zeus,
I've tried this and so far it seems to work....just having problems with some other code.
Thanks for your help!
-
Nov 17th, 2009, 02:53 PM
#8
Re: Popping up a messagebox using ASP.Net???
Hmmm, I am going to have to try this out.
Just away to install Visual Studio on my PC. This would be news to me if this works.
Gary
-
Nov 17th, 2009, 03:55 PM
#9
Thread Starter
PowerPoster
Re: Popping up a messagebox using ASP.Net???
Ok,
For some reason it's not working now. I was wondering if this will work in the code-behind file for the master page? On my master page, I have a menu control. In my code-behind file, I have a Menu3_MenuItemClick event. I'm assuming that this event will fire when a menu item is clicked on. I've attached the code below:
Code:
Imports System.Messaging
Partial Class site2
Inherits System.Web.UI.MasterPage
Public blnLoggedIn As Boolean = False
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Catch ex As Exception
End Try
End Sub
'
'
'
Protected Sub Menu3_MenuItemClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MenuEventArgs) Handles Menu3.MenuItemClick
Try
MsgBox("You selected " & e.Item.Text)
If blnLoggedIn Then
Else
'Select Case e.Item.text
' Case "Message Board"
' MsgBox("Please log in first!")
'End Select
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OkOnly)
End Try
End Sub
End Class
The Red highlighted code is not executing.
Thanks,
-
Nov 17th, 2009, 04:05 PM
#10
Re: Popping up a messagebox using ASP.Net???
Hey,
Have you set a break point on that line?
Does it get hit in the debugger?
Gary
-
Nov 17th, 2009, 05:55 PM
#11
Thread Starter
PowerPoster
Re: Popping up a messagebox using ASP.Net???
Yes, I set a breakpoint and it still doesn't hit it...
-
Nov 18th, 2009, 02:15 AM
#12
Re: Popping up a messagebox using ASP.Net???
Hey,
That would suggest that the method is never getting called.
Out of curiosity, can you try the syntax here:
http://msdn.microsoft.com/en-us/libr...ck(VS.80).aspx
i.e. declare the event handler in the aspx markup, rather than in the server side code behind.
Gary
-
Nov 18th, 2009, 12:54 PM
#13
Thread Starter
PowerPoster
Re: Popping up a messagebox using ASP.Net???
Gary,
I tried that and it still didn't work. Do I need to use the code exactly as it was in the example? I named my menu control "menu3" as the ID. So I'm thinking the event needs to be menu3_MenuItemClick(). What do you think?
Thanks,
-
Nov 18th, 2009, 12:59 PM
#14
Re: Popping up a messagebox using ASP.Net???
Hey,
The actual event handler can be called anything that you want it to. As long as the
definition of the event handler matches.
Are you saying the breakpoint still does not get hit?
Gary
-
Nov 18th, 2009, 01:10 PM
#15
Thread Starter
PowerPoster
Re: Popping up a messagebox using ASP.Net???
Ok,
Here is what I have in the aspx markup:
HTML Code:
<script runat="server">
Sub NavigationMenu_MenuItemClick(ByVal sender As Object, ByVal e As MenuEventArgs)
MsgBox("Please login first!", MsgBoxStyle.Information)
End Sub
</script>
I have the following line of code in the code-behind file.
Code:
Public Event MenuItemClick As MenuEventHandler
-
Nov 18th, 2009, 02:39 PM
#16
Re: Popping up a messagebox using ASP.Net???
Hey,
Are you using the inline coding model? I thought you were using the code behind model?
Is that all the code you have?
In this instance, you are not hooking up the Event to the Event Handler. You are missing a line like the following:
Code:
AddHandler menu2.MenuItemClick, AddressOf NavigationMenu_MenuItemClick
You can see an example of this in the following:
http://msdn.microsoft.com/en-us/libr...nthandler.aspx
I think this is another one of those situation where you need to strip it back to basics.
Create an application that just has a menu control. Select it in the designer, go to the properties window, click the yellow thunderbolt, find the MenuItemClick event, then double click it. That will hook up the event handler for you, so you don't have to worry about it.
Then pop your MsgBox in that.
Does that work?
Gary
-
Nov 18th, 2009, 03:04 PM
#17
Thread Starter
PowerPoster
Re: Popping up a messagebox using ASP.Net???
Gary,
I did what you suggested and created another app with just a menu control. I put the msgbox in the MenuItemClick event and it worked, however, the msgbox did not show up on the screen rather in the application tray in a minimized state. When I clicked on it, only then did it popup as a regular msgbox does.
-
Nov 18th, 2009, 03:06 PM
#18
Thread Starter
PowerPoster
Re: Popping up a messagebox using ASP.Net???
Nevermind Gary....I got it working. For some reason it wasn't popping up like it should have but now it is. This is in the new app by they way...
-
Nov 18th, 2009, 03:12 PM
#19
Re: Popping up a messagebox using ASP.Net???
Hey,
I am really not sure what to say. I have never used this method.
Having said that, I have just fired open my newly installed Visual Studio, and did what I suggested above, and it seemed to work just fine.
I really don't know what MsgBox is doing under the hood though, I would much prefer to be in direct control of what is going on using what I suggested earlier, or do it using RegisterClientScript:
http://www.beansoftware.com/ASP.NET-...ssage-Box.aspx
Gary
-
Nov 18th, 2009, 03:19 PM
#20
Addicted Member
Re: Popping up a messagebox using ASP.Net???
As long as an app is running on a machine (localhost), the Messagebox syntax does in fact work. However, once you upload it to a web server, the functionality will disappear.
Why does it work like this? Sounds like a good question for Microsoft.
As far as the thread goes, I just use some scripting in my code-behind. The AJAX modal pop-up works as well, but I tend to stay away from AJAX as much as possible.
If my post helped you, please rate it!
Languages: VB/ASP.NET 2005, C# 2008,VB6
Databases: Oracle (knowledge not currently in use), DB2
FROM Customers
WHERE We_Know_What_We_Want <> DB.Null
SELECT *
0 rows returned
-
Nov 18th, 2009, 03:23 PM
#21
Re: Popping up a messagebox using ASP.Net???
Originally Posted by Blakk_Majik
As long as an app is running on a machine (localhost), the Messagebox syntax does in fact work. However, once you upload it to a web server, the functionality will disappear.
I believe that this is because it will only show the MessageBox on the server machine that is hosting the application. Another reason to stay away from it.
Originally Posted by Blakk_Majik
but I tend to stay away from AJAX as much as possible.
Why is that?
Gary
-
Nov 18th, 2009, 03:29 PM
#22
Thread Starter
PowerPoster
Re: Popping up a messagebox using ASP.Net???
Gary,
How do you normally display messages in your web apps?
Thanks,
-
Nov 18th, 2009, 03:34 PM
#23
Re: Popping up a messagebox using ASP.Net???
Hey,
Historically, I have used essentially a JavaScript alert, wrapped up in a RegisterClientScript block, as I linked to. Or, I have just written out to a Label on the web form itself.
Going forward though, I am starting to use the AJAX toolkit to do it.
Gary
-
Nov 18th, 2009, 03:42 PM
#24
Thread Starter
PowerPoster
Re: Popping up a messagebox using ASP.Net???
Gary,
I just started writing message out to a Label control on the master page. However, in testing this, it still doesn't seem to hit the code-behind file. Basically, the test I am running is that when someone clicks on a menu item, the menu_MenuItemClick event should fire checking a boolean variable to see if the user is logged in or not. If they're not logged in, then a message should display in the Label telling them to log in. It's not even doing that and that's about as simple code as I can think of.
I will say this, I have a "Page_Load" event in the code-behind file that sets the Label.Text value to "" and that seems to work when the page loads. It's just the menu_MenuItemClick Event that's not firing....
-
Nov 18th, 2009, 03:45 PM
#25
Re: Popping up a messagebox using ASP.Net???
Hey,
I can only think then that your event handlers are not hooked up correctly in the application you are running.
Can you do the same thing in the sample project that you made? i.e. where the MsgBox was kind of working, can you right to a label in the MenuClickEvent? If so, then there is something wrong in the application you are trying to run.
It is difficult to tell without seeing the whole application though.
Can you delete the menu control and start again?
Gary
-
Nov 18th, 2009, 03:47 PM
#26
Re: Popping up a messagebox using ASP.Net???
Originally Posted by Zeuz
But Msgbox("Works with me in ASP.NET")
Acutally it doesn't. Try opening your site on a different machine and you would be able to see what is actually happening.
The msgbox is a server side function. So the message box would be shown on the server instead of the client browser. Your code would remain blocked until the ok button is clicked and the client would timeout waiting for a response if it is not clicked. And that's not what you really want.
The correct way would be to use AJAX or simple javascript alert function.
e.g. if there is a button named btnTest on your form, and you want to show the messagebox on clicking that button, do this in Page_Load (or whatever event you find appropriate):
vb.net Code:
btnTest.Attributes("onclick") = "javascript:alert('hello!');return false;"
-
Nov 19th, 2009, 02:31 AM
#27
Re: Popping up a messagebox using ASP.Net???
Hey Pradeep,
Can't agree more
Gary
-
Nov 21st, 2009, 05:54 PM
#28
Re: Popping up a messagebox using ASP.Net???
This isn't a question for Microsoft. This is a question for yourself - have you looked at the namespace that MsgBox falls under? Or MessageBox.Show for that matter?
These are Windows Forms related functions. There's a reason the javascript alert() is the recommended way
-
Nov 22nd, 2009, 08:10 PM
#29
Thread Starter
PowerPoster
Re: Popping up a messagebox using ASP.Net???
mendhak,
Then how would I could the statement in post #26? Basically, when the user clicks on a button (if he is not logged in), a message needs to popup letting them know they are not allowed to access that link.
The way I've thought it out is to set a boolean variable to true if a member of the site has logged in. When a menu button is clicked, a check needs to be made against the boolean variable. How would I do this using that statement in post #26?
Thanks,
-
Nov 23rd, 2009, 03:38 AM
#30
Re: Popping up a messagebox using ASP.Net???
Originally Posted by blakemckenna
mendhak,
Then how would I could the statement in post #26? Basically, when the user clicks on a button (if he is not logged in), a message needs to popup letting them know they are not allowed to access that link.
The way I've thought it out is to set a boolean variable to true if a member of the site has logged in. When a menu button is clicked, a check needs to be made against the boolean variable. How would I do this using that statement in post #26?
Thanks,
There are many other ways to do this like using the ClientScript.RegisterStartupScript etc. to insert the javascript code, but this is just the basic way to do this (in continuation of post #26):
Just give your form body element an ID and add the runat="server" tag to it, so that we can refer to it in code-behind.
Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
...
</head>
<body runat="server" ID="PageBody">
...
</body>
</html>
Then the rest of it is similar to what I posted in post #26
vb.net Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not IsLoggedIn Then '' replace with whatever way you use to authenticate the user. PageBody.Attributes("onload") = "javascript:alert('User is not logged in!');return false;" End If End Sub
Last edited by Pradeep1210; Nov 23rd, 2009 at 03:42 AM.
-
Nov 23rd, 2009, 03:41 AM
#31
Re: Popping up a messagebox using ASP.Net???
Originally Posted by Pradeep1210
There are many other ways to do this like using the ClientScript.RegisterStartupScript etc. to insert the javascript code, but this is just the basic way to do this (in continuation of post #26):
Just give your form body element an ID and add the runat="server" tag to it, so that we can refer to it in code-behind.
html Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
...
</head>
<body runat="server" ID="htmlBody">
...
</body>
</html>
Then the rest of it is similar to what I posted in post #26
vb.net Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsLoggedIn Then '' replace with whatever way you use to authenticate the user.
PageBody.Attributes("onload") = "javascript:alert('User is not logged in!');return false;"
End If
End Sub
Hey Pradeep,
Do you mean:
Code:
htmlBody.Attributes("onload") = "javascript:alert('User is not logged in!');return false;"
Gary
-
Nov 23rd, 2009, 03:45 AM
#32
Re: Popping up a messagebox using ASP.Net???
okk I missed the menuitem part. But that is no different than what is in post #26. just replace the btnTest with your menuitem ID.
e.g.
vb.net Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not IsLoggedIn Then '' replace with whatever way you use to authenticate the user. YourMenuItem.Attributes("onclick") = "javascript:alert('User is not logged in!');return false;" End If End Sub
-
Nov 23rd, 2009, 03:48 AM
#33
Re: Popping up a messagebox using ASP.Net???
Ah, sorry, I see what you are getting at.
That makes sense!!
Although, to me, it still makes more sense for the menu item to simply not appear if the user does not have access to it. That is why I suggested restricting the Menu items based on the role of the logged in user.
Gary
-
Nov 23rd, 2009, 09:03 AM
#34
Addicted Member
Re: Popping up a messagebox using ASP.Net???
Originally Posted by gep13
I believe that this is because it will only show the MessageBox on the server machine that is hosting the application. Another reason to stay away from it.
Thanks for that tidbit of info gman. Never really knew WHY it worked like that (too lazy to look it up), but now I do.
As far as Ajax goes, I don't like spending much time in the "aspx" part of the app. Seems like when I use Ajax, I spend as much time fiddling around with the page code as I do the code-behind.
I dunno. I always felt like Ajax was something that was "thrown on top" of ASP.NET. It never felt like it was integrated properly.
If my post helped you, please rate it!
Languages: VB/ASP.NET 2005, C# 2008,VB6
Databases: Oracle (knowledge not currently in use), DB2
FROM Customers
WHERE We_Know_What_We_Want <> DB.Null
SELECT *
0 rows returned
-
Nov 23rd, 2009, 09:15 AM
#35
Re: Popping up a messagebox using ASP.Net???
Hey,
Interesting to hear that side of it.
I am coming into AJAX quite late, only nearly playing with it properly in Visual Studio 2008 and now in 2010, and I think it is now far more mature than where it used to be.
Gary
-
Nov 23rd, 2009, 11:53 AM
#36
Thread Starter
PowerPoster
Re: Popping up a messagebox using ASP.Net???
Ok,
So the code in post #31 goes in the master page markup and the code-behind file correct?
Thanks,
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|