Javascript alert() after Response.Redirect - c#

I am calling this in my code behind:
(test.aspx)
Response.Redirect("~/Default.aspx");
I want to include a javascript alert after/before being redirected to Default.aspx, is it possible?
I'm doing this because I'm passing a value to another page (test.aspx) and that page checks the db, if reader HasRow(), then redirect to Default.aspx.

The way to do that is display the alert with javascript and then do the redirect with javascript as well:
ScriptManager.RegisterStartupScript(this,this.GetType(),"Redit","alert('asdf'); window.location='" + Request.ApplicationPath + "Default.aspx';",true);

Lets take a look at what happens when you call response.redirect()
Servers sends HTTP response 302 to browser including the url to navigate to
browser doesn't display content of response and instead gets the content from the url specified
if the request to the new url succeeds, then it is displayed.
Now looking at this, we can deduce that it is impossible to tell the browser to do a alert() from the page that issues the redirect because its content (if any) is discarded.
It is possible to accomplish what you want from the page that you are redirecting to. To do this, just check Request.UrlReferrer to check if you were redirected from the correct page, then display the alert when appropriate.
example:
Page1 redirects to page2
Page2 check if Request.UrlReferrer is equal to page1
If equal, display alert
If not, do nothing special
Another approach is do the alert first then do the redirect from javascript. window.location.href = newurl.

if you have display something message on Defaul.aspx page you must declare it. Because when you use redirect your page is rendering from top. You must set before redirect Sesion state on something flag and on Default.aspx page you must insert section who been added when this Sesion state been set.

Related

Need to render form in footer only on home page. Asp.net MVC

I have a _layout.cshtml page that renders the header and footer for every page. In my _footer.cshtml I have a newsletter form. As of right now, the newsletter form is rendering on every page. I need it to only render on the home page footer.
My thought for a solution is just using an if statement that checks if we're on the home page:
#if(Request.RawUrl == "/")
{
Newsletter form
}
My problem with this is that whenever I try to get the url, it's the same on every page.
For example:
the url might be https://something.com/checkout, so Request.RawUrl gets /checkout. But when I navigate to https://something.com, Request.RawUrl still gets /checkout. How can I get the actual path of the page?
I've tried using just about every request.url option I can find. None of them get the actual url path of the page after being redirected to a new page. Just whatever the original one was.
The best way would be to use a ViewModel instead of querying the Request object.

ASP.NET Page Cycle Confusion

If I have a button on my ASP.NET page which will take you to another page but that page will do something and then will send user back using
Uri uu= Request.UrlReferrer;
if (uu!= null)
Response.Redirect(uu.ToString());
Now Which page event can I use so that when other pages displays I can display a message box.
In short I am running my custom code in a "aspx" page where user is directed on button click, and then after custom code I am sending user back to old page, but it happens so quickly that user doesn't realize that he went on another page, now I want to display a message box after redirect on same page user started from, what to do :S !
More Information
EDIT
Sorry guys but I can't make changes to ASP page where button is at all
:(
I'm a bit unsure about how the Request.UrlReferrer gets set. I think it's a browser implementation detail. So I wouldn't trust on that.
I would go for something like
A.aspx -> Redirects to -> B.aspx
B.aspx -> Redirects to -> B.aspx?message=1
And check if message=1 is set.
But if you want to use the Request.UrlReferrer it should be accessible on Page_Load
If you use it this way, it'll never appear to the client.
Maybe try redirecting back using javascript with a delay so user can be informed
The best thing to do it's add a flag to the redirected page so you can show something special when the flag is turned on
Uri uu= Request.UrlReferrer;
if (uu!= null)
Response.Redirect(uu.ToString() + "?Message=DataHasChanged");
and then in the ASP page
<% if (Request.QueryString["Message"] == "DataHasChanged") { %>
<div class="alert">The data has changed. Please review it or whatever</div>
<% } %>

asp.net: how do i prevent users from posting the same data multiple times

i have a little asp.net web application.
it is a front end to a sql-server-2008 database.
after they fill out all the data, they will press submit and the data will be sent to the database.
after this point if the user refreshes the page, the data is posted again!!! how do i disable this from happening?
Send a Location header back to the user that redirects the browser to another page: refresh will then reload that other page rather than resubmit the old form.
This is caused by the last request being a POST request to the page, what do you need to do is a redirect so the last request becomes a GET.
After you have handled the post data you can just do a redirect to the same page with:
Response.Redirect("~/ThePage.aspx");
This will prevent you from presenting a message to the user straight from the code behind, if you want to present a success message using this method you will need to add a querystring or something similar:
Response.Redirect("~/ThePage.aspx?result=success");
And then check on the page bind if the querystring to present a success message is set, such a check could look something like this:
if (Request.QueryString["result"] != null && Request.QueryString["result"].ToString().ToLower() == "success")
{
//Show success message
}
Another solution which probably is superior but might require some more work is to wrap the form in a updatepanel, you can read more about it here: http://ajax.net-tutorials.com/controls/updatepanel-control/
An updatepanel will make the form submit with AJAX instead of full postback.
You need to follow the Post/Redirect/Get pattern which is explained on WikiPedia and alluded to by Femi. In your code after you've done your processing do a Response.Redirect to the desired page.
See this article about the PRG pattern: http://en.wikipedia.org/wiki/Post/Redirect/Get
In short, after the user POSTs (submits) data to your server, you issue a Response.Redirect to have the users browser GET a page. This way, if the user presses the reload button, it is the GET request that is repeated.

Displaying a server-side alert message and then redirecting to a new page

I have a form on my page where users can send an email,
try
{
smtpClient.Send(message);
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Key",
"alert('Thank you for your submission.');", true);
}
catch(Exception)
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Key",
"alert('There was an error processing your request.');", true);
}
Response.Redirect("home.aspx");
The alert does not appear and the page simply redirects immediately, unless I comment out Response.Redirect, then the alert works. I want to display the alert and then redirect.
How can this be achieved? I thought of using a timer somehow, but I'm not sure if thats the best solution.
Thanks.
Let the JS do the redirect, instead of using Response.Redirect.
eg.
alert('....'); window.location='home.aspx';
The reason is that the RegisterClientScriptBlock call and the redirect code are within the same page life cycle. That means Response.Redirect() get invoked before the page rendered to the client's browser. The client has no way to see alert message.
The solution is use JS to redirect page as suggested by Will

ASP.net Redirect to the calling page

I have a page that calls another page with some query string parameters. I want to return back to that page after clicking on a button.
I have to mention that I write that code in a user control and I don't know what page called that second page.
Is there something like Back button in browsers?
Simplest way use javascript on client side with
window.back();
For server side you need to save the url referer in page_load:
if(!Page.IsPostback)
{
ViewState["GoBackTo"] = Request.UrlReferrer;
}
and on a button click using Response.Redirect:
Response.Redirect( ViewState["GoBackTo"].ToString() );
edit: please note ppumkin's comment below!
You could look at Cross Page Posting.
Alternatively, if you are generating the link programatically you could include the returnUrl in the url e.g. http://localhost/secondpage.aspx?returnurl=firstpage.aspx
You can then read this querystring parameter in the secondpage and perform as redirect back once your work is done.
You can use the Request.UrlReferrer, but it is not necessarily sent from the client all the time:
Response.Redirect(Request.UrlReferrer.AbsoluteUri);
put this line of code on the page load event
Btn_Back.Attributes.Add("onClick", "javascript:history.back(); return false;");

Categories