Button redirect error - c#

I am trying to a redirect the user when they click on a specific button
protected void Button1_Click(object sender, EventArgs e)
{
Server.Transfer("ControlPanel/Default.aspx");
}
The problem is when I click Button1 it redirects me to another page
localhost:57988/WebSite5/Default.aspx
and the weirdest thing is it open another page with this link above, not the default page I have, but another but with the default.aspx page url that you see!
Any suggestions?

You are not doing a redirect, you are doing a transfer. That means that the execution continues with the new page, but the URL doesn't change. The page that you transferred to is returned as response to the request for the first page.
Use Response.Redirect instead of Server.Transfer to do a redirect.

When you use Server.Transfer it won't display the new URL in the query string. Could it be that is what's throwing you off?
Here is a good article on Response.Redirect vs Server.Transfer. If you really want to redirect the user then you should use Response.Redirect.

Related

How to change after postback URL in asp.net 4.5 web forms

Here a button that does a postback
<asp:LinkButton runat="server" ID="btnBestPrice" OnClick="btnSearchBestPrice_Click">Search Best Price</asp:LinkButton>
Assume that this button is clicked on page
http://localhost:47207/Default?ClusterId=131
Now after the postback is completed, the page is still
http://localhost:47207/Default?ClusterId=131
However, after the postback, i want to make URL to be
http://localhost:47207/Default
Is that possible?
If i make a redirect, the postback event would be loss. I still want to process the postback event perfectly fine. So if somehow i can set the postback url to be raw url of the page on the client side or?
asp.net 4.5 web forms c#
I assume your postback is displaying some information to user, but have to change the URL without interrupt the process.
First of all you have to understand that if you changed the URL in the server side, the browser will treat it as a new page and make a new request. Response.Redirect is basically telling the browser it is time to move onto another page. So you cannot change the URL while remaining in the same request. (while Server.Transfer is remaining at the same URL but different page which is not what you want)
So I have 2 solutions for you, the following one make sense to me but there is still redirecting the page:
protected void Page_Load(object sender, EventArgs e) {
if (Session["ClusterId"] != null) {
try {
int ClusterId = int.Parse(Session["ClusterId"]);
// Code here
} catch { }
Session.Remove("ClusterId");
return;
}
}
protected void btnSearchBestPrice_Click(object sender, EventArgs e) {
int ClusterId = int.Parse(Request["ClusterId"]);
Session.Add("ClusterId", ClusterId.ToString());
Response.Redirect("~/Default");
}
Here is another solution that does all the actions in your btnSearchBestPrice_Click event without Redirect and Session, and bind a JavaScript page ready event, call history.pushState and also wipe out the unnecessary parameters in the action attribute of your form element.

Response.Redirect not working on WAP sites in a basic phone model

When i click on the Submit Button and on the Button Click i have written a Response.Redirect to a new web page it firstly prompts a message.
Data you are sending will be redirected to another server. Continue?
If i say yes it gives an error Response Unknown.
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Default2.aspx",false);
}
Try Server.Transfer("Default2.aspx");
This function save double trip to the server.
Response.Redirect() sends a redirection header to the client, and the client itself requests the new page.
Server.Transfer() stops rendering the current page and starts rendering another one.
Try not to use the "false" only write:
Response.Resirect("Default2.aspx");

how to call one webpage from another webpage in asp.net

I have 2 web applications. webapp1 is running at location say - weblocationlocation1/webapp1/default.aspx
and webapp2 is running at different location say -
weblocationlocation2/webapp2/default.aspx
Now, If I want to call webapp2/default.aspx from webapp1 then how to call.
how to run Page_Load(object sender, EventArgs e) of webapp1 from webapp2/default.aspx.
I have to stay on webapp1/default.aspx in my browser. and still want to load webapp2/default.aspx (ONLY from my code of button clicked). in this case, how to store cookie/session variables. and want to maintain them in webapp1 across all pages.
If you want to do this via a redirect then:
Response.Redirect("weblocationlocation2/webapp2/default.aspx");
Or directly on the server use
Server.Transfer("weblocationlocation2/webapp2/default.aspx");
Or
Server.Execute("weblocationlocation2/webapp2/default.aspx");
The last will return control to the calling method (the second won't).
as described by # Justin Harvey you can use Page_load() method and call Response.redirect method to redirect to your desired web page
You can also use javascript if you want to redirect to your page on event such as button on click
for that you can do following
btn_demo_onClick()
{
window.location = "abc.aspx";
}
it just a complementary option if you want to go with javascript
Thanks
Response.Redirect("default.aspx"); // At URL You will Get the default page as what you are redirecting to.
Server.Transfer("default.aspx"); // At URL You will not Get the default page as what you are redirecting to.
example : If you are logged in Login page then you want to redirect to default page ,then you can use both the above mentioned methods.

How to redirect from one ASP.NET page to another

How do I redirect from one ASP.NET page to another ("Webform2.aspx") by means of a button?
You can redirect from one page to another using Response.Redirect()
set PostBackUrl property of button, like this :
button1.PostBackUrl= "Webform2.aspx";
You can redirect to another ASP.NET page using the code below :
Response.Redirect("Webform.aspx");
This is the simplest way
Personally, if all you're wanting to do is load a new page when a button is clicked, I would do this with client-side script.
You could use a JS library for this (eg: jQuery), like so:
jQuery
$(function() {
$('#<%= button1.ClientID %>').click(function() {
window.location.href = "Webform2.aspx";
});
});
ASP.NET
<asp:Button id="button1" runat="server"/>
Or, for a specifically ASP.NETesque way to do it, you can use Button.PostBackUrl as Antonio suggests, which still uses client-side script but means you don't have to write it yourself. The HTML for the button renders as:
<input type="submit" name="button1" value="Button" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("button1", "", true, "", "webform2.aspx", false, false))" id="button1" />
If you've got other processing to do server-side and you need to redirect afterwards, use
Response.Redirect("Webform2.aspx"); in your click handler.
If that's not working for you, please add some more detail to your question to explain what's happening.
Well there are lot of ways. Response.Redirect, Server.Transfer, Javascript call to the page.
Javascript call is required when u have no server side actions for the button.
onclick="javascript:window.location.href = Webform2.aspx?id='<%=Request.QueryString["id"]%>'"
Server.Transfer will do a re-direct at server side. i.e, The browser will still show after the response from webform2. Webform1.aspx will re-direct the request to webform2 and webform2 will give the req. (Req = 1, Res = 1)
Response.Redirect: webform1 will send a response asking the browser to make a new request to webform2. In this case, the browser will change the url as it is making a new req to webform2.(Req = 1 + 1, Res = 1+1)
There is one more way, form.submit() if you are interested. The traditional html form submit.
Forgot to mention the best of all, the cross-page postback with PostBack url..
http://aspdotnetcode.source-of-humor.com/TipsAndTricks/General/CrossPagePostbackAspNetCrossPagePostback.aspx
You can use below code :
protected void Button1_Click(object sender, EventArgs e) {
Response.Redirect("default2.aspx");
}
Notice that default2.aspx is your second web page name and you
Response.Redirect(string url) issues a 302 HTTP status code instructing the client to redirect to url. The browser will issue a new request for url and the URL will change in the address bar.
Server.Transfer(string path) terminates execution of the current page and starts execution of a new page on the specified path i.e. internally within IIS. Therefore the URL in the browser address bar will not be changed. The page you transfer to must be an aspx page in the same web site.
The differences are subtle but important. A simple way to think about this is to ask yourself "should the user bookmark/favorite this URL?". Use Response.Redirect if the URL has changed and future visits to the content should be on the new URL. Use Server.Transfer if the URL is correct and current but you need to display different content this one time - maybe you are displaying an error message or you need the user to enter their credentials to continue or there is some other reason why the content should change but the URL should not.
Either of the above can be used within the Click event handler of an ASP.NET Button control in your code-behind:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Webform2.aspx");
// OR
Server.Transfer("Webform2.aspx");
}
Both Response.Redirect and Server.Transfer methods are used to transfer a user from one web page to another web page. Both methods are used for the same purpose but still there are some differences as follows.
The Response.Redirect method redirects a request to a new URL and specifies the new URL while the Server.Transfer method for the current request, terminates execution of the current page and starts execution of a new page using the specified URL path of the page.
Both Response.Redirect and Server.Transfer has same syntax like:
Response.Redirect("UserDetail.aspx");
Server.Transfer("UserDetail.aspx");
Before touching on more points I want to explain some HTTP status codes, these are important for the understanding of the basic differences between these two. The HTTP status codes are the codes that the Web server uses to communicate with the Web browser or user agent.
Response.Redirect sends an HTTP request to the browser, then the browser sends that request to the web server, then the web server delivers a response to the web browser. For example, suppose you are on the web page "UserRegister.aspx" page and it has a button that redirects you to the "UserDetail.aspx" web page.

Response.Redirect causes download of the swf

I have a flash image slider with a button below each image.
When i press that button, the user is redirected to a new page
where i add that image product to my cart.
The problem is that after doing the adding, i want to redirect the user back to the initial page.
The code:
protected void Page_Load(object sender, EventArgs e)
{
addProductToBasket(getCategoryIdFromUrl(), getProductIdFromUrl());
Response.Redirect(Request.UrlReferrer.ToString());
}
Please note that in Firefox is working fine but in IE or Chrome it is DOWNLOADING the swf...If i comment Response.Redict(...) the user remains on this page so the click button is working well, only the redirect seems to be the problem.
Any suggestions please?
Edit: The problem seems to be that Request.UrlReferrer keeps as link not the initial page containing the swf but the swf itself....
So, instead of doing redirect to:
http://localhost:1336/Site/Index.aspx
if does redirect to the swf contained on the Index.aspx page
http://localhost:1336/carousel/carouse.swf
Solved: with a session variable where i keep the initial page's url
It seems to me that it's a function of the flash player setting the referrer header differently in different browsers.
If that is the case, then you might want to have the flash player get the url of the page it is hosted on, and pass that as a parameter to your page, and then redirect to the contents of the parameter.
What I think you really should be doing though is registering an HttpHandler (IHttpHandler implementation) for the URL which performs the processing and creating a response which returns JSON or XML, which flash can easily parse.
This way, you don't have a page reload and you have a seamless experience.

Categories

Resources