Search code examples
c#asp.netpopup

How do I create a popup message for a webpage?


I've been searching for a way to create a popup message to alert the user on a webpage that there was an error in execution. I'm new to this, so I don't have much knowledge on what is or is not possible, but here's what I've explored so far.

  • Winforms: not compatible with webpages
  • MessageBox: doesn't work at all due to throwing up a red error line when used in code. Not sure why it's doing that.
  • Convert Winforms to .exe: exe can't be called on webpage

I'm debugging a website right now and I don't want it taking the user to an error page. I want this to pop up and then send them on their way, but I can't even figure out how to make this happen.

Is this possible? If not, what is the easiest solution you would recommend to use to alert the user that not everything was executed properly?

Please, treat me like I don't know anything. Take me step by step through this. I'm just trying to find any solution at this point.


Solution

  • Well, first of all, displaying some type of popup message as opposed to error handling are HUGE AND MASSIVE different issues.

    So, a grasp of the architecture here is required.

    Any windows commands, such as message box? Well, they can be I supposed used during debug and testing on the "same" computer, but anything "windows" related in terms of a web browser and web server? They should not be used in the same sentence.

    So, what does occur if you did (for some strange reason) say use a windows msgbox command in your web code?

    Say this code:

        protected void Button1_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.MessageBox.Show("test");
    
        }
    

    Well, that msgbox going to appear on the computer running the web code!!!

    So, msgbox has ZERO to do with a web browser. You can "force" the web enviroment to use a msgbox (you have to set a reference to system.Windows.forms).

    So, the code you write runs on the same computer that is running the web server (in this case IIS (Internet information Services).

    The result would be if we zoomed in on the computer console in the computer room, you would see on that SAME computer running the web server (that can dish out pages to any web browser).

    The result would be a msgbox on this computer screen:

    enter image description here

    Sames goes for say Debug.Print("some text") - again, that's going to appear on the computer running the code. that is the computer/server running the web site, NOT the end users computer. Remember, the computer running the web site is NOT the users computer!!

    You can't really touch much on the end users computer - including their screen/montior, or even touch or grab ANY thing from files or anything else - including that of writing out files. The end user browser is "sand boxed", and if you could write to, read from, or do anything to the end users computer? Then no one would use the web due to security concerns.

    So, the first issue as a general rule you can't just "pop a dialog" on the end users web browser. What you "have" to do is SEND a WHOLE web page to the end user.

    So, that means the user can't see/get/have a dialog box "appear" on their existing page, but you would have to send a WHOLE new page.

    Now, it is actually possible to adopt some type of web socket technology, and have some kind of dialog pop up in the end users web browser?

    Yes, it is, but you would have to in fact cobble together a mix of client side JavaScript code, and then some server side code (using web-sockets). Since this can be a RATHER complex and VERY advanced type of coding? (you would have to write your own communication protocol).

    Then most people will adopt what is called the SignalR frame work. This a .net library "helper" code that consists of some client side JavaScript code, and server side code (using web sockets). It allows messaging, and is also say how a site can have 2 users "chat" to each other in real time.

    So, while it is possible, you would have to adopt signalR for this.

    You can learn about SignalR here:

    https://dotnet.microsoft.com/en-us/apps/aspnet/signalr

    However, one would not walk into a hospital and go to the 8th floor to start doing open heart surgery, or brain surgery WHEN just starting out in medical school, would they? Same goes for web land development. You don't really want to start out using "advanced" technology from day one.

    Well, if one is just starting out in web development land? I would simple configure IIS web services to "display" or navigate to a web page that states an error occurred. In fact, what I do is send them to a page called "myProjects". (a page that displays their current active and live projects).

    I also then of course generate a email and send it to myself (and the other developer). I mean, as you note, it don't make sense for a end user to get/see a messy web page error.

    So, what the end user "will see" when your code errors out?

    It depends on quite a few settings. For one, are you running the deployed site in debug mode, or in release mode? (that matters).

    Then there are some security settings, since when a web site errors out, you the developer(s) may well want to see the error message that the WEB server (IIS services) spits out. however, during production use, then of course you do NOT want users to see such a error message). thus by default, display of the error messages are tuned off, and the user will not see (much) details in terms of the error.

    So, in web.config, you can control WHAT displays, and even what page the user is sent to.

    So, say this:

    <system.web>
    
      <customErrors mode="Off"/>
    

    Now, what occurs DEPENDS on if you running site in debug mode, or release mode.

    In debug mode, then the debugger will break on that line of code (debug mode="off")

    so, say this code:

            <asp:Button ID="Button1" runat="server" 
                OnClick="Button1_Click" Text="Test Button"
                Width="110px" />
    

    Then we get this when we click on that button:

    enter image description here

    However, if I change from debug to release, then I get/see this:

    enter image description here

    So, for now? I would probably just setup a page to "re-direct" the users to. And in most cases they tend to then not realize a error occurred.

    and you can set above to off, or On, or RemoteOnly.

    RemoteOnly:

    For a local user (developer) then you get the web page error, but remote users don't, and you can re-direct them to any web page you want.

    eg:

    <customErrors mode="On" defaultRedirect="~/Default.aspx"/>
    

    However, I tend to use none of the above, and in Global.aspx, then I handle the error that way.

    I get/grab/save the error message, send a email to the developer team, and then as noted re-direct the page - the user never really sees ANY error message, but only is jumped to their live projects page.

    So, in summary:

    It's probably a WHOLE lot less hassle and learning curve to just send users to some page - could be a error page - that's your choice.

    Could you have a pop up dialog appear? yes, but then you would have to adopt some web socket technology to do this, since a "push" type of notification to a existing web page is a challenge, but is possible if you adopt SignalR.

    So, there are "bloatloads" of great web dialogs you can adopt, but that of triggering or display of a popup on a web form is a common everyday occurrence. However, trapping a code error running on the server and triggering a dialog in the browser is quite a different "use case" here, and as noted, it is possible, but few go down that road.

    Edit: adding the global.aspx page

    If it already exists, then you DO NOT get the option to do this.

    So, right click on the project tree, choose add new item.

    This option:

    (vb)

    enter image description here

    The same should work for c#.

    Do keep in mind I don't believe this option exists for a web site, but ONLY for a asp.net web site "application"