Results 1 to 36 of 36

Thread: Popping up a messagebox using ASP.Net???

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

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

  2. #2
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: Popping up a messagebox using ASP.Net???

    You have to use javascript to show an alert box.

    HTML Code:
    1. <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.
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  3. #3
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  4. #4
    Hyperactive Member
    Join Date
    May 2008
    Location
    >> ( ҉ )
    Posts
    413

    Re: Popping up a messagebox using ASP.Net???

    Msgbox("Like this?")

  5. #5
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  6. #6
    Hyperactive Member
    Join Date
    May 2008
    Location
    >> ( ҉ )
    Posts
    413

    Re: Popping up a messagebox using ASP.Net???

    But Msgbox("Works with me in ASP.NET")

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

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

  8. #8
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

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

  10. #10
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Popping up a messagebox using ASP.Net???

    Yes, I set a breakpoint and it still doesn't hit it...
    Blake

  12. #12
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

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

  14. #14
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  15. #15

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    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
    Blake

  16. #16
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  17. #17

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

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

  18. #18

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

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

  19. #19
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  20. #20
    Addicted Member
    Join Date
    Dec 2006
    Location
    Between Try & Catch
    Posts
    249

    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

  21. #21
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Popping up a messagebox using ASP.Net???

    Quote Originally Posted by Blakk_Majik View Post
    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.

    Quote Originally Posted by Blakk_Majik View Post
    but I tend to stay away from AJAX as much as possible.
    Why is that?

    Gary

  22. #22

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Popping up a messagebox using ASP.Net???

    Gary,

    How do you normally display messages in your web apps?

    Thanks,
    Blake

  23. #23
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  24. #24

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

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

  25. #25
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  26. #26
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Popping up a messagebox using ASP.Net???

    Quote Originally Posted by Zeuz View Post
    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:
    1. btnTest.Attributes("onclick") = "javascript:alert('hello!');return false;"
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  27. #27
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Popping up a messagebox using ASP.Net???

    Hey Pradeep,

    Can't agree more

    Gary

  28. #28
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,173

    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

  29. #29

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

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

  30. #30
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Popping up a messagebox using ASP.Net???

    Quote Originally Posted by blakemckenna View Post
    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:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.     If Not IsLoggedIn Then     '' replace with whatever way you use to authenticate the user.
    3.         PageBody.Attributes("onload") = "javascript:alert('User is not logged in!');return false;"
    4.     End If
    5. End Sub
    Last edited by Pradeep1210; Nov 23rd, 2009 at 03:42 AM.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  31. #31
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Popping up a messagebox using ASP.Net???

    Quote Originally Posted by Pradeep1210 View Post
    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:
    1. <html xmlns="http://www.w3.org/1999/xhtml">
    2.     <head>
    3.         ...
    4.     </head>
    5.     <body runat="server" ID="htmlBody">
    6.         ...
    7.     </body>
    8. </html>

    Then the rest of it is similar to what I posted in post #26
    vb.net Code:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.     If Not IsLoggedIn Then     '' replace with whatever way you use to authenticate the user.
    3.         PageBody.Attributes("onload") = "javascript:alert('User is not logged in!');return false;"
    4.     End If
    5. End Sub

    Hey Pradeep,

    Do you mean:

    Code:
    htmlBody.Attributes("onload") = "javascript:alert('User is not logged in!');return false;"
    Gary

  32. #32
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.     If Not IsLoggedIn Then     '' replace with whatever way you use to authenticate the user.
    3.         YourMenuItem.Attributes("onclick") = "javascript:alert('User is not logged in!');return false;"
    4.     End If
    5. End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  33. #33
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  34. #34
    Addicted Member
    Join Date
    Dec 2006
    Location
    Between Try & Catch
    Posts
    249

    Re: Popping up a messagebox using ASP.Net???

    Quote Originally Posted by gep13 View Post
    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

  35. #35
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  36. #36

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width