displaying a popup window in asp.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • btreddy
    New Member
    • Oct 2008
    • 83

    displaying a popup window in asp.net

    Hii all ,

    I'm implementing one mail sending application ..in that once the user entered the server details and credentials ..it has to send the mail to the respective user..

    Here in the Send button click event im implementing all the logic...if the user enters the wrong data/provide no data i need to alert the user with a popup saying that you have not enterd the data ...

    How to display tht popup in asp.net ..on client side i know how to call the popups using javascript..but how to implement the same on server side..

    Thanks in advance.

    Rgds,
    BTR.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Your Server Side code can't produce message windows/alerts (what you're referring to as popups). It can dynamically produce JavaScript though... and JavaScript can display alerts :)

    So, when your server code detects that something doesn't validate it can dynamically produce JavaScript that can be called during the client side onload event (when the page is sent back to the browser).

    It gets a bit trickier when you're using Ajax but it's doable.

    Comment

    • sangam56
      New Member
      • Nov 2007
      • 68

      #3
      Hi btreddy,
      You would like some kind of windows forms like messagebox in asp.net web page. This could help you implement a messagebox function which you can raise anytime during server processing.

      Hope this helps. Please feel free to ask if the problem persists of if any futher help is required. Thanks. Happy Programming!

      Comment

      • balame2004
        New Member
        • Mar 2008
        • 142

        #4
        Cool! try this.

        Response.Write( "<script language='javas cript'>alert('M essageBox')</script>");

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Originally posted by sangam56
          Hi btreddy,
          You would like some kind of windows forms like messagebox in asp.net web page. ...
          Thanks for the link sangam! It's really informative.

          The only thing is that it's probably not a good idea to be writing JavaScript into Labels as suggested by the article because the web application could be configured to use Server.HtmlEnco de on all incoming/outgoing text.

          This method will substitute characters that have HTML meaning into their ascii equivalent to protect the server and end user from any malicious code that may have been retrieved...

          For example if you set the text of a label to the following:

          myLabel.Text = "<script>alert( 'Hello World!');</script>"

          It will normally be rendered as:
          Code:
          <span id="myLabel">
            <script type="text/javascript"> alert('Hello World'); </script>
          </span>
          The angled brackets ( <> ) are special characters in HTML: they are used to indicate elements. So the Browser sees that there's a script tag and executes the JavaScript with it.

          But if the server is using the Server.HtmlEnco de() method the label will be rendered as:
          Code:
          <span id="myLabel">
           &#38;#60; script type="text/javascript" &#38;#62; alert('Hello World');  &#38;#60; /script &#38;#62;
          </span>
          The angled brackets are replaced with their ascii equivilent and the browser prints the script in the page instead of executing JavaScript code.

          Originally posted by balame2004
          Cool! try this.
          Response.Write( "<script language='javas cript'>alert('M essageBox')</script>");
          This is getting a bit closer because the HtmlEncode method isn't going to mess with the JavaScript however when you use the Response.Write method in your VB or C# code it can end up anywhere in the HTML...

          This means that it's likely that the JavaScript will be written outside of the HTML....

          For example, if you were to use the above Response.Write method it would probably place the JavaScript as such in the HTML:
          Code:
          <script language='javascript'>alert('MessageBox')</script>
          
          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
              <title></title>
            </head>
            <body>
              <!-- your page content goes here -->
            </body>
          </html>
          This makes the HTML invalid because the JavaScript is out side of the document. The JavaScript should be placed in the <head> section or <body> section:

          For example:
          Code:
          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
              <title></title>
              <!--
                  The script can be placed here or in the body to make it valid
               -->
                <script language='javascript'>//alert('MessageBox')</script>
            </head>
            <body>
              <script language='javascript'>alert('MessageBox')</script>
              <!-- your page content goes here -->
            </body>
          </html>
          The Response.Write( ) method should only ever be called from the ASP code...instead of being used in your C# or VB code behind.

          So, both of solutions may or may not work...dependin g on server configuration and how/where the JavaScript is written in the HTML (and how the Browser treats the invalid HTML).

          I would recommend something completely different.
          Create a Protected Property that indicates whether or not the alert needs to be displayed. A property with a Protected scope will make it available to the ASP code. You can use the Response.Write( ) method in the ASP code to ensure that it is writing to a valid place in the HTML....and dynamically create your script as such....

          (Please note that <%= %> is the ASP short hand for <% Response.Write( ""); %>...
          Also please note that any thing in <% %> is executed on the server...so this should be C# code or VB.NET code...in this case I'm using VB.NET and so there are no semicolons (;) in the <% %> tags but if you were using C# you would need to use proper C# syntax)


          ASP code:
          Code:
          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
              <title></title>
            </head>
            <body>
              <script language='javascript'>
                var shouldDisplayAlert = '<%= ShouldDisplayMessage() %>';
                if(shouldDisplayAlert == 'true' || shouldDisplayAlert == 'True'){
                  alert( '<%= TheMessage() %>');
                }
              </script>
          
            </body>
          </html>
          In the VB or C# code you would have to define 2 properties to make the above code work:
          • the ShouldDisplayMe ssage property that returns true or false to indicate whether or not to display the message
          • and the TheMessage property which returns a string containing the message to display



          VB code:
          Code:
          Private _message As String ="" 'This string can be set any where in your Code Behind to say anything you want to say in the message box
          
          Private _displayMessage As Boolean = False 'This boolean indicates whether or not the message box should be displayed...it can be set anywhere in your code behind as well.... 
          
          Protected Property TheMessage As String
            Get
               return message
            End Get
            Set (ByVal value As String)
              message = value
            End Set
          End Property
          
          Protected Property ShouldDisplayMessage As Boolean
            Get
               return displayMessage 
            End Get
            Set (ByVal value As Boolean)
              displayMessage = value
            End Set
          End Property

          You could also use the methods available to you in the Page.ClientScri pt class to register dynamic JavaScript with the page so that it is placed properly in the HTML....

          Or you can use the ScriptManager if your site is Ajax Enabled to register your Scripts....or even the ScriptManagerPr oxy class.

          There are a lot of ways to do it but I find that the above technique is easiest in a lot of cases.

          See how to use JavaScript in ASP.NET for an example of how to use the Page.ClientScri pt to register your dynamic JavaScript with the page.

          Comment

          Working...