Solved

popup error message in asp.net

Posted on 2008-10-23
24
1,938 Views
Last Modified: 2010-04-21
you guys know that msgbox doesn't work in the asp.net  so what do i do to have a popup message when there's an error. if you have the code also that would be great. thanks.
0
Comment
Question by:StewSupport
  • 11
  • 7
  • 5
  • +1
24 Comments
 
LVL 3

Expert Comment

by:TechSinger
ID: 22786171
The way I have done this is to use the Javascript command, Alert.   If you need to have the message come up during server side code, ti will cause a repost.
It is much cleaner if can build the whole part of the code that needs the message in Javascript, then it won't require a new post since the code is running on the client side.
0
 
LVL 10

Expert Comment

by:Bane83
ID: 22786208
From javascript, you would use alert.

<script type="text/javascript">
alert("There was an error!");
</script>

You cannot do this directly from ASP.NET, but you can write the script to the page using the ClientScriptManager (I believe the method you need is Page.ClientScriptManager.RegisterClientScriptBlock).  You'll can pass this some javascript like the one above and it will write it to the page and pop up the alert.
0
 

Author Comment

by:StewSupport
ID: 22786904
when i tried this code, it doesn't have any pop up at all
Dim tail As String
Dim scs As String
Dim guts As String
 
tail = " </" & "script>" 'Note wierd construct required
guts = "function testing() {confirm('trying script); return false; }"
scs = "<script type='text/javascript'> " & guts & tail
 
Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "myscript1", scs)

Open in new window

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 10

Expert Comment

by:Bane83
ID: 22787069
Right, you put it inside of a function, so you need something to call it.  You can either pull it out of the function or add a line to guts which will call the function, ie:

guts = "function testing() {confirm('trying script); return false; } testing();"
0
 

Author Comment

by:StewSupport
ID: 22787300
yes i do have something that call that function but the popup just doesn't pop
0
 
LVL 10

Expert Comment

by:Bane83
ID: 22787418
Ah, I just tried it out and got an unterminated string literal error here:

confirm('trying script);

You're missing the closing quote:

confirm('trying script');
0
 

Author Comment

by:StewSupport
ID: 22788581
still doesn't popup on mine
0
 
LVL 10

Expert Comment

by:Bane83
ID: 22788618
Could I see some of the page code?  How are you attempting to call testing()?  Is it on a button click?  Do you use any UpdatePanels on the page?
0
 

Author Comment

by:StewSupport
ID: 22788939
i use updatepanel and a button to click. once you click the button it calls the function above which has javascript that calls that the javascript function
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22789411
>>>i use updatepanel and a button to click. once you click the button it calls the function above which has javascript that calls that the javascript function
So you want to run the server side code first, then the JavaScript code, right?
0
 
LVL 10

Expert Comment

by:Bane83
ID: 22789443
Ah, ok, there's the problem.  If you have an UpdatePanel, you can't use Page.ClientScriptManager.RegisterClientScriptBlock you need to use ScriptManager.RegisterStartupScript
http://msdn.microsoft.com/en-us/library/bb359558.aspx
0
 

Author Comment

by:StewSupport
ID: 22799867
yes prairedog i want to run the server side code first then javascript if there's an error.
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22799885
What do you want to do after displaying the error message?
0
 

Author Comment

by:StewSupport
ID: 22813422
just hit ok really.
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22823314
>>>just hit ok really.
Then what? Stay on the same page or redirect user to another page?
0
 

Author Comment

by:StewSupport
ID: 22889274
then stay on the same page.
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22889376
OK, in your code behind, whereever you find an error, do this:
If (errorFound) Then
   ClientScript.RegisterStartupScript(Me.GetType(), "AlertBox", "alert('Error found.');", True)
Else
   'Continue your work
End If
0
 

Author Comment

by:StewSupport
ID: 22904354
nope, i dont see any popup at all.
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22905739
Can you post your code to let me see where you put the code snippet?
0
 

Author Comment

by:StewSupport
ID: 22906033
right at the page load i dont  i was just testing  the code so right when pageload i have the code. nothing else.
0
 
LVL 22

Accepted Solution

by:
prairiedog earned 500 total points
ID: 22906098
You mean like this and it is not working?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        
	ClientScript.RegisterStartupScript(Me.GetType(), "AlertBox", "alert('Error found.');", True)
 
End Sub

Open in new window

0
 

Author Comment

by:StewSupport
ID: 22917743
yes that is correct
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22918121
Hmmm, it is strange....it is working with me. Maybe you can post your code here?
0
 

Author Closing Comment

by:StewSupport
ID: 31509200
i dont know what it is i reinstalled vb and it worked with your script. thank you for helping.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question