how to call one webpage from another webpage in asp.net - c#

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.

Related

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.

HttpRedirect on every link within FBML application, why?

I have set up the SDK on my FB application but for the life of me cannot work out why the redirection happens.
The app is an IFrame so, for testing I have two pages, on page one a link to page two, when I click the link the whole page is redirecting as opposed to the IFrame src redirecting.
Both pages are checking to see if the user is logged in with the following code..
protected string requiredAppPermissions = "user_about_me,email";
protected FacebookApp fbApp;
protected CanvasAuthorizer authorizer;
protected void Page_Load(object sender, EventArgs e)
{
fbApp = new FacebookApp();
authorizer = new CanvasAuthorizer(fbApp);
authorizer.Perms = requiredAppPermissions;
if (authorizer.Authorize())
{
}
}
I have had a look in source and can see this in the FacebookAppRedirectHttpHandler, I just can't understand why you would want to keep redirecting the full page for every navigation link?
The most important reason is that Facebook passes the authentication to the signed_request to the source on every request. It either does this with a POST in the body or with a GET in the querystring. The reason we do this is because cookies aren't 100% reliable. If we redirected inside the iframe we would have to store the user's session in a cookie. Some browsers, including safari, don't let iframe apps create cookies. There are ways around this, but for most people the way we have it works best. If you want to have a redirect inside the iframe without changing the top url you will have to save the session in some way and pass it to the second page. You could do this by adding it to the querystring (complicated) or storing it in the Session (not very scalable) or using cookies (not reliable).

Button redirect error

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.

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.

Dynamic Pages From a Database in C#

Forgive me if this has already been asked somewhere, but I cannot figure out the best way to accomplish this task. I want to be able to create a rendering system that will allow me to render out content from thousands of different .aspx pages without having to create thousands of .aspx pages. That being said, I still want to be able to render out the appropriate .aspx page if it exists in my code.
For example, when a request is made to the site, I want to check and see if that URL is in the database, if it is, then I want to render the content appropriately. However, if it doesn't, then I want it to continue on to rendering the real .aspx page.
In trying to use an HTTPModule, I cannot get the page that exists in the database to write out the appropriate content. Here's my code.
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication;
Uri url = application.Context.Request.Url;
//Checks to see if the page exists in the database
PageInformation page = PageMethods.GetPageFromUrl(url.AbsolutePath);
if (page != null)
{
string renderedPage = Renderer.RenderPage(page);
application.Context.Response.Write(renderedPage);
}
}
However, when trying to use an HTTPHandler, I can't get the real .aspx pages to render appropriately because the *.aspx verb is being dealt with by the handler.
If anyone has any better ideas on how to completely re-design this, I'm completely open to that as well. Thanks.
This will do the trick:
Type page_type = BuildManager.GetCompiledType ("~/page.aspx");
Page page = (Page) Activator.CreateInstance (page_type);
page.ProcessRequest (Context);
I think you're lookign for a simple URL rewriting example.
So you have a single page "default.aspx" that could take an argument of the content you want to display "default.aspx?page=home", but you don't want the nasty query string part "?page=home".
this is best solved by URL rewriting which can be used as an ISAPI module in IIS. So instead of the URL string above, people see a page called "home.aspx", and the web server translates this into "default.aspx?page=home" for your page which can go get the content for the "home" page out of the DB and display it on the screen.
Here's a page with more information on a good implementation of this process:
http://www.opcode.co.uk/components/rewrite.asp
I believe this shows how to process the "normal" pages inside a handler
other example

Categories

Resources