How do I do a cross page postback from codebehind? - asp.net

I want to do something similar to what happens when you click an asp.net button that has a PostBackURL set. I've tried Server.Transfer but the URL doesn't change (which is something I want). Is there a better way to do this, or alternatively is there a way to make Server.Transfer display the correct URL?

Try Response.Redirect
UPDATE:
You can't do a proper postback from codebehind to my knowledge I'm afraid

See:
Cross-Page Posting in ASP.NET Web Pages
How to: Post ASP.NET Web Pages to a Different Page
asp:Button
ID="Button1"
PostBackUrl="~/TargetPage.aspx"
runat="server"
Text="Submit" />
Edit:
You could also construct a HTTP POST in the codebehind and send it to the target page then write the response out to the browser. This won't change the URL in the brower's address bar to the actual page that the data was POSTed to. What you can do depends on what control you have. Is the page that is POSTed to under your control? Do it contain any text/data that is specific to what is POSTed to it? You could do the POST and then redirect/transfer to the target page but that may or may not display the result of the POST correctly.
How to use HttpWebRequest to send POST request to another web server may be of some help if you decide to go this way.

Related

Use PostBackUrl to submit specific part of form

I have an ASP.NET webforms application and a requirement to add a form to a page which will be posted to an external URL, a payment processing provider. The form needs to include specific hidden inputs, one of which is a hashed string representation of the form data.
I understand given limitations of webforms I can't nest a second form within the main <form runat="server" />. However, I don't have the option to locate my form outside of that main form (due to the CMS this site is built into).
I know I can use an ASP.NET Button control with a PostBackUrl attribute which allows the form to post to a specific URL. However, this posts every form element on the page, including __VIEWSTATE, __EVENTTARGET etc.
This is not desired behaviour. I only want to submit specific data. Also, this makes it difficult (impossible?) to generate the hash representation of the form because this would have to include viewstate etc.
I also don't have the option to post the data from the code behind because the client is expected to continue their interaction at the target URL.
Do I have any options here? This must be possible, but an internet search has returned very little.
UPDATE: I'm looking for a solution that doesn't rely on Javascript.
One option would be to utilize ajax. Simply use it to post data you need and you don't even have to redirect or anything. You'd be in control of literally everything (well as long as the user has javascript turned on, of course).
You can use ajax update panel for this .
put asp:scriptManager on page
<asp:scriptManager></asp:scriptManager>
<asp:updatePanel runat="server">
<contentTemplate>
your controls to be submitted to next page
<asp:button><asp:button>
</contentTemplate>
</asp:updatePanel>
and put other controls outside updatePanel.

How a browser can reach the server without doing postback?

i've been asked this question and id not know the answer.
Thanks for any help!
Postback is a term used often in ASP.NET when a WebForm POSTs the single form back to the server and invokes some event in the code behind (like a click on a button for example). You could still use normal GET requests though to redirect to a given web page. For example you could use an anchor:
Go to page 2
When the user clicks on the anchor there is no postback occuring but a GET request to the target web page.
Another possibility is the user typing directly the address of the web page in his browser address bar.
Yet another possibility is to use javascript to perform an AJAX request which allows to invoke a web page without redirecting away from the current page. You could use any HTTP verb with AJAX.
We can use javascript code to do some function without postback. This will save the time for the request and response to the server. But this client side. you can't reach the server without posting back.But my mean you can do functionality by javascript which does not postback the page.
Hope it may help.
If you like to categories the call to the server you can say that there are two types.
The GET and the POST
The POST is the post back and are the parameters that you send using a form
and the GET that are the parameters that you can send from the url.
More about:
http://www.cs.tut.fi/~jkorpela/forms/methods.html
http://thinkvitamin.com/code/the-definitive-guide-to-get-vs-post/
http://catcode.com/formguide/getpost.html
but I think the interview question was about the Ajax call, and this is probably what they try to see if you know, how to use Ajax to reach the server with javascript and not make postback. But you need to know that Ajax can make POST back, but this is done with out leave the page, with out make a full page post back.

Simple form POST from HTML to ASP.NET page fails

Ultimately I have to send form post data from an iPad app to a simple ASP.NET page. Before I do that I just want to get the basic ASP.NET page working by sending a simple form post from an HTML page I have directly to the asp.net page. I post the html form to the asp.net page, and the Request.Form object is always null. I know the page is being hit, because the debugger stops on my breakpoints in the codebehind (.cs).
I know that if I sent the form post from the .aspx page it would work; that's the traditional asp.net form post model. But if the page won't process a post from an arbitrary html page then I believe it will also fail when the post comes from the ipad.
This is puzzling to me. Does ASP.NET somehow discriminate on form posts? Does it somehow know that the post didn't originate from its own aspx, and ignore the post? What is going on and how do I solve this? Thanks in advance.
Yes, ASP.NET does discriminate on form POSTs. If you set up an ASP.NET form normally and then use a tool such as Fiddler to see exactly what is being posted, then you will see all the hidden fields and values that ASP.NET requires for that page. Then, you'll be ready to send data from a non-ASP.NET source.

Cross-Page Posting with a Master Page

I found a workaround for my specific need, but I thought I'd ask this question anyway.
Say I have a typical data entry web project with a master page -- instead of using the Session variable and using Response.Redirect or Server.Transfer to redirect users who are part of the way through data entry to the next step, I'd rather use cross-page posting.
I tried setting up one of my websites in this manner, with a button like:
<asp:Button ID="next" text="next" runat="server" PostBackUrl="EnterInfo.aspx" />
When I went to test the changes, my <form> tag hadn't changed at all:
<form id="aspnetForm" action="SelectUser.aspx" method="post" name="aspnetForm">
Did I miss any details here, or is cross-page posting simply not intended for use with a master page?
Edit:
The form tag above is the tag as rendered on the client -- not the server-side tag. I've read MSDN articles (like this one) that seem to me to explicitly state that cross page posting actually posts the form to another page.
Perhaps I've misinterpreted this as changing the form's action, but regardless my source form does not post invoke anything anywhere on my target when I click the button I altered -- it merely posts back to the current page.
I don't think using the PostBackUrl attribute will change the action of the form in the markup, as there could be many other controls just doing a normal postback. I imagine that some javascript is used to change the action of the form when this particular button is pressed.
You need to place a declaration on the second page where data come from.
So you have:
PostBackUrl="EnterInfo.aspx"
On EnterInfo.aspx you declare where you can get informations
<%# PreviousPageType VirtualPath="~/SelectedUser.aspx" %>
and you get them by...
if (Page.PreviousPage != null)
{
if(Page.PreviousPage.IsCrossPagePostBack == true)
{
GetTheClass = PreviousPage.MyDataClass;
}
}
The PostBackUrl not change the form url.
Some reference.
ASP.NET how to access public properties?
Cross-page postbacks and back again retaining data from source page
Ps
At first I was thinking that you have just type what you see at render, but if not the case As #Mystere point out you need to run this inside an asp.net form.
First, as Graham says.. PostBackUrl does not change the postback action. Second, you can only use PostBackUrl on an asp.net enabled form, which means the form must have runat="server"

ASP.NET submit post request with keys from code behind

I am working on a page where we have a form that the user is filling some information in. When the form is submitted I want to process the information and then redirect to an external server. The catch is that I need post data to go to the external server so a redirect will not work.
Is there a way to programmatically submit a form request with post data?
For the sake of a ridiculous example let's say on http://A.com I have an asp.net page with two inputs that accept numbers and a submit button. When the button is clicked I want to send a post to http://B.com with a post data parameter "AdditionTotal" which contains the sum of the two numbers entered.
Absolutely, and it's very easy to do.
Normally a page posts back to itself, but you can override this by changing the "PostBackUrl" property of an ASP.NET button.
<asp:Button
ID="Button1"
PostBackUrl="http://B.com/AdditionTest.aspx"
runat="server"
Text="Submit" />
What you can do is something similar to what you're already doing (ie in your responst to Damien). However, put the data in hidden fields, and then submit the form via javascript on the onload. I know it's not very elegant, but if you need to post the data to the other URL, that's the only way you can get the browser to post the data.
You could post the data yourself, but there is no way to translate that back to the users browser to send them to it as well. Posts have to happen client side for the browser to display the response page.

Resources