473,499 Members | 1,681 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

displaying a popup window in asp.net

83 New Member
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.
May 27 '09 #1
4 14273
Frinavale
9,735 Recognized Expert Moderator Expert
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.
May 27 '09 #2
sangam56
68 New Member
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!
Jun 2 '09 #3
balame2004
142 New Member
Cool! try this.

Response.Write("<script language='javascript'>alert('MessageBox')</script>");
Jun 2 '09 #4
Frinavale
9,735 Recognized Expert Moderator Expert
@sangam56
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.HtmlEncode 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:
Expand|Select|Wrap|Line Numbers
  1. <span id="myLabel">
  2.   <script type="text/javascript"> alert('Hello World'); </script>
  3. </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.HtmlEncode() method the label will be rendered as:
Expand|Select|Wrap|Line Numbers
  1. <span id="myLabel">
  2.  &#60; script type="text/javascript" &#62; alert('Hello World');  &#60; /script &#62;
  3. </span>
The angled brackets are replaced with their ascii equivilent and the browser prints the script in the page instead of executing JavaScript code.

@balame2004
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:
Expand|Select|Wrap|Line Numbers
  1. <script language='javascript'>alert('MessageBox')</script>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5.   <head>
  6.     <title></title>
  7.   </head>
  8.   <body>
  9.     <!-- your page content goes here -->
  10.   </body>
  11. </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:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3.   <head>
  4.     <title></title>
  5.     <!--
  6.         The script can be placed here or in the body to make it valid
  7.      -->
  8.       <script language='javascript'>//alert('MessageBox')</script>
  9.   </head>
  10.   <body>
  11.     <script language='javascript'>alert('MessageBox')</script>
  12.     <!-- your page content goes here -->
  13.   </body>
  14. </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...depending 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:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3.   <head>
  4.     <title></title>
  5.   </head>
  6.   <body>
  7.     <script language='javascript'>
  8.       var shouldDisplayAlert = '<%= ShouldDisplayMessage() %>';
  9.       if(shouldDisplayAlert == 'true' || shouldDisplayAlert == 'True'){
  10.         alert( '<%= TheMessage() %>');
  11.       }
  12.     </script>
  13.  
  14.   </body>
  15. </html>
In the VB or C# code you would have to define 2 properties to make the above code work:
  • the ShouldDisplayMessage 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:
Expand|Select|Wrap|Line Numbers
  1. 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
  2.  
  3. 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.... 
  4.  
  5. Protected Property TheMessage As String
  6.   Get
  7.      return message
  8.   End Get
  9.   Set (ByVal value As String)
  10.     message = value
  11.   End Set
  12. End Property
  13.  
  14. Protected Property ShouldDisplayMessage As Boolean
  15.   Get
  16.      return displayMessage 
  17.   End Get
  18.   Set (ByVal value As Boolean)
  19.     displayMessage = value
  20.   End Set
  21. End Property
  22.  

You could also use the methods available to you in the Page.ClientScript 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 ScriptManagerProxy 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.ClientScript to register your dynamic JavaScript with the page.
Jun 2 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
18271
by: Noozer | last post by:
When using the WebBrowser control, is it possible to cause popup windows to appear within the WebBrowser control itself instead of a new window? This is what I've written in the NewWindow2 event,...
3
12852
by: Jeanne | last post by:
I am working on a cgi script that is suppose to pop-up a javascript box from the following perl variables:$TodayDate, $LinkCity, $LinkState. I recently encountered a problem with the $LinkCity...
2
4346
by: Moon | last post by:
Seems I still haven't got the hang of all those window generating code in Javascript. I've got a page with about 15 photo thumbnails. When you click on a thumbnail a new window pops up which shows...
4
22152
by: Davey | last post by:
I have a website which has a popup window (this only opens when the user chooses to open it). In the popup window I have a <select> control which lists a selection of "classes". Each class has a...
3
7367
by: | last post by:
I have a pdf that is contained in a byte array in memory and I want to display this in a browser window. The window will beopenned when a button is clicked on the current web page, i.e. a second...
3
1707
by: Rusty | last post by:
Hi, our intranet web site needs to add this feature. I've got the components but just need the last step to get it going. Here's the setup. 1) a user clicks on a link which calls a web service...
7
3645
by: anthony.turcotte | last post by:
Hi, I've looked for a solution to this problem on google, read posts in this newsgroup and I still haven't found anything that could help me. Here's the scenario. 1. User accesses...
5
9008
by: debbiedchang | last post by:
Hi, I have a custom user login authentication page. There are times when the login authentication succeeds, but before I redirect to the home page, I want to display a popup with a warning message...
5
5991
by: kenethlevine | last post by:
Hello I am fairly new to Access and am having a problem for which I am trying everything without success. It is access 2003. I have a main form. When the user presses a button a modal popup...
0
7006
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6892
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7385
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5467
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1425
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
661
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.