Link to home
Start Free TrialLog in
Avatar of 66chawger
66chawgerFlag for United States of America

asked on

Send Message Box or Alert to User inside of my ASPX

I need to display message box or alert and redirect a user to a specific page when checking the querystring in my aspx.  See my code snippet for what I currently have working, which is only the re-direct.  I need to send the message dialog/box before the redirect.
<%if (Request.QueryString["QuoteID"] !=null)
------>  need to put alert or message box here or some way
	Response.Redirect ("../SafecoAccess/QuoteCommercialAuto.aspx?p_no=313&Dest=QQ&QuoteID="+psQuoteID);
       
%>

Open in new window

Avatar of anoyes
anoyes
Flag of United States of America image

Because ASP.net is a server-side language, there's no way for it to directly with the browser.  There are a couple of ways to get around this.  One is to have .net inject javascript into the page to display the alert dialog box.  The down side of this is that it requires two page loads..1 to inject the javascript and show the alert, and then a 2nd to redirect.  If you don't mind the additional post-back then this is a viable option.  In my opinion, however, a far more effective method would be to use the ASP.net AJAX ControlToolkit, which includes a modal popup as part of the control set.  There are several advantages to using this over the standard javascript alert box.  The biggest is that you have complete control over the contents and appearance of the alert, as all it's really doing is showing a div. The other is that you can make calls to it from your code behind, so you can do something like Alert1.Show() without a post-back required.

Will this dialog box be called based on a button push?  Because, if so, there's a great article here at 4GuysFromRolla that talks about doing just that.
http://aspnet.4guysfromrolla.com/articles/021104-1.aspx
Avatar of 66chawger

ASKER

anoyes, thanks for your response.

No, I am doing this on page initiation, no button or action.  

I definitely agree with the AJAX solution, and I will do that soon, however this is a hot issue I need to get in immediately.   I have coded the following, only if I have the response.redirect directly after the response.write to print the alert, I never see the alert.  Do I need a wait?  I assume the code is just falling through.  This is definitely not the way I want to do this, but for now I need a quick and dirty.

<%if (Request.QueryString["QuoteID"] !=null)	
Response.Write("<script langauge=\"javascript\">window.alert('This works')</script>");
 
Response.Redirect ("../SafecoAccess/QuoteCommercialAuto.aspx?p_no=313&Dest=QQ&QuoteID="+psQuoteID);
 
%>

Open in new window

Instead of Response.Write, try wrapping the JS using ClientScriptManager.RegisterStartupScript

ClientScriptManager.RegisterStartupScript(this.GetType(),"AlertDialog","<script langauge=\"javascript\">window.alert('This works')</script>");
Is the ClientScriptManager... still inside the asp block <% %>?  What about the redirect?  How do I keep that from occurring until the alert is closed (ok is pressed)?

Sorry, some days I feel like I just heard of .NET yesterday!  The wrapper idea is thought though.
Ah, sorry.  No, the ClientScriptManager... would go in your page's PageLoad function.  I have another thought tho.  How about doing the whole redirect using javascript.  That will ensure that the alert is dismissed before the redirect occurs.  You need to add a PageLoad event to your page, which will replace what you've got in the <% %>.  Your page load will then check the same logic, and spit the JS out if necessary.

if (Request.QueryString["QuoteID"] !=null) {
   ClientScriptManager.RegisterStartupScript(this.GetType(),"AlertDialog","<script langauge=\"javascript\">window.alert('This works');window.location='../SafecoAccess/QuoteCommercialAuto.aspx?p_no=313&Dest=QQ&QuoteID='+psQuoteID;</script>");
}

Give that a shot and see if it works.
LOL!!  I knew there was a way to next the two...I bounce back and forth between Java and .NET frequently...this was not that obvious, but I do feel a bit sheepish not figuring this out.  Let me give it a go and see what happens.
Ok, got the old "Newline in Constant" message when trying to split up this script.    Also, I have the private void Page_Load event in my code behind.  What replaces the contents of the <%%> in the aspx?  Or are you saying that the Page_Load in the code behind will replace having to do using asp script in the aspx?
Also, not recognizing the namespace ClientScriptManager, do I need another reference.  FYI this is a web.ui.page
Guess what.....forgot to mention that my app is in .Net 1.1, the ClientScriptManager is in 2.0....
So I still have the same issue, having the redirect happen only after the alert message has been displayed.
ASKER CERTIFIED SOLUTION
Avatar of anoyes
anoyes
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is my problem.  First, the re-direct is still not taking place.  Secondly, there is a re-direct to a usercontrol within my aspx, this happens and then I see the alert, but the redirect associated with the alert does not take place.  At the bottom of the aspx there is an <OBJECT defined, this comes write after the </head>.  I would think that by either calling a method from server script at the top of the html (like I was originally trying to do) or having the code in the Page_Load, this would occur first?
I couldn't get the syntax correct either, compiler doesn't like the quotes around AlertScript.
This is why I had created a method earlier today
I have public string declared and a method to use it.  Then I call the method CheckForRedirect() in the <%%> at the top of the aspx page.  but....no re-direct!

See my code snippets.
private string javaScript = "<script language= \"JavaScript\">window.alert('Quote ID is null'); window.location='../SafecoAccess/QuoteCommercialAuto.aspx?p_no=313&Dest=QQ&QuoteID='+psQuoteID;</script>";
 
Method:
 
public void CheckForRedirect()
{
  Page.RegisterStartupScript("AlertScript",javaScript);
}

Open in new window

From the code snippet you just posted, it looks like you're checking to see if QuoteID is null and if it is display an alert and redirect.  If this is what you're trying to do, you're not checking for the right condition.  In your first post: if (Request.QueryString["QuoteID"] !=null)...if QuoteID is NOT null.  I think what you want here is actually if (Request.QueryString["QuoteID"] ==null)
Anoyes, actually the condition I am checking is correct for what I need to accomplish, if the QuoteID is not null (meaning it has a value).  So, if the quote is NOT null, I want to do the alert and re-direct.  I know it may sound strange, but this is the way I need it to work.

Anyway, any ideas on why the re-direct is not taking place?
Are you sure that the PageLoad is firing?  I was able to do this using the javascript above, and just a page load that did registerstartupscript.  As I understand it, you want to check on page load to see if the quote id is not null, and if it's not null, display an alert and redirect, yes?  Do you have AutoEventWireup set to true?  Are you getting javascript errors?  Can you maybe post all the code you have?  I have a feeling that maybe the pageload event isn't firing.
Good questions!  I do have a Page_Load in the code behind, which was generated by .NET when the form was created.  I assume this is the Page_Load event you are speaking of.   There is existing code in the Page_Load already so I assume you mean to put this at the beginning.   This is some older code and I would not have coded this way..trust me, so don't laugh to hard at the code I am sending.  I will attach the aspx and the aspx.cs.    Thanks again for your help, I know I have used/done what you are suggesting before.   Always something!!!....Oh, that was my original reason for wanting to embed the javascript in <%%> at the top of the html.   Now, you will have to elaborate on the AutoEventWireup, I think I understand what you mean, but just in case.....  
SafecoCommercialAuto-aspx.txt
SafecoCommercialAuto-aspx-cs.txt
AutoEventWireup basically just automatically wires events like Page_Load, Page_Init, etc. to the events w/o you having to actually code the event handlers...if that makes sense.  AutoEventWireup is false in your page (it's right in the first line), but the designer already wired it up when it created the event, so no worries there.

Anyhow, just for humor...let's try this.  I was able to get it working using 2.0 w/ 1.1 functions, but I see no reason why it shouldn't working using the native 1.1.  I unfortunately don't have VS 2003 or before so I can't compile natively in 1.1, but, here's what I suggest.

From the actual page, remove all of this:
      <%if ((Request.QueryString["ClientID"] ==null)&& (Request.QueryString["QUOTEID"] ==null))
      Response.Redirect("../../QuoteVault/client_list.asp?ProdID=313&P_ID=313&pt_id=1");%>
      
      <!--Added code to redirect client to Quote Summary page when selecting edit from ADB-->
      
      <!--Response.Write("<script language=\"javascript\">window.alert('This quote application is not available for edit.  You will be redirected to the quote proposal documents. If you need to change risk/coverage information, please complete a new quote, or contact customer service at (800) 272-7550.');window.location='../SafecoAccess/QuoteCommercialAuto.aspx?p_no=313&Dest=QQ&QuoteID='+psQuoteID;</script>");-->
      <%if (Request.QueryString["QuoteID"] !=null)
            Response.Write("<script language=\"javascript\">window.alert('This quote application is not available for edit.  You will be redirected to the quote proposal documents. If you need to change risk/coverage information, please complete a new quote, or contact customer service at (800) 272-7550.');window.navigate='../SafecoAccess/QuoteCommercialAuto.aspx?p_no=313&Dest=QQ&QuoteID='+psQuoteID;</script>");
            
      %>

In your page load, change the RegisterStartupScript bit to:
        string AlertScript = "<script language='javascript'>alert('hello');</script>";
        Page.RegisterStartupScript("AlertBox",AlertScript);

This should just pop an alert box when the page loads.  I want to be sure that this executes and then we can go from there.  I see no reason from the code you gave me why this wouldn't work.  So let's start simple and make sure it works.
Ok, now I only have the string AlertScript... at the very beginning of the page_load.  Yes, the alert fires, but the redirect(not the redirect we want to do after the alert, this is another which happens during normal flow of the aspx...don't get confused....)  to the usercontrol object at the bottom of the aspx page has already taken place.  The only way I could figure out how to have the alert pop up was by ebedding it in asp at the top of the aspx.  The alert would always fire in the code behind whether I put it in the pag_load or called the method from the aspx, however, like I said that re-direct to the usercontrol object at the bottom of the aspx page fires first.    If I could just get the alert and then upon 'OK' do the re-direct that we have been attempting I could go from there.
Ok, finally figured out why the redirect was not occurring, I was not concatenating the parameter "psQuoteID" correctly at the end of the string:

.....QQ&QuoteID=" +psQuoteID + "';  

psQuoteID needed to be inside of double quotes with the + sign at the beginning and end.  
Worked fine!   I am accepting your solution because you gave me so much input! Thanks again!
Solution was correct, I just needed to adjust the syntax on my end.  Great Job!