I know in VB.Net I can use "messagebox.show". How do I popup a message box in ASP.Net?
Thanks,
Printable View
I know in VB.Net I can use "messagebox.show". How do I popup a message box in ASP.Net?
Thanks,
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.
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
Msgbox("Like this?")
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
But Msgbox("Works with me in 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!
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
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:
The Red highlighted code is not executing.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
Thanks,
Hey,
Have you set a break point on that line?
Does it get hit in the debugger?
Gary
Yes, I set a breakpoint and it still doesn't hit it...
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
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,
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
Ok,
Here is what I have in the aspx markup:
I have the following line of code in the code-behind file.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>
Code:Public Event MenuItemClick As MenuEventHandler
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:
You can see an example of this in the following:Code:AddHandler menu2.MenuItemClick, AddressOf NavigationMenu_MenuItemClick
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
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.
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...
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
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.
Gary,
How do you normally display messages in your web apps?
Thanks,
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
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....
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
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;"
Hey Pradeep,
Can't agree more :)
Gary
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 ;)
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.
Then the rest of it is similar to what I posted in post #26Code:<html xmlns="http://www.w3.org/1999/xhtml">
<head>
...
</head>
<body runat="server" ID="PageBody">
...
</body>
</html>
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
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
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
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.
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
Ok,
So the code in post #31 goes in the master page markup and the code-behind file correct?
Thanks,