tags:

views:

184

answers:

4

Sorry, another super basic ASP.NET question. this so embarrassing.

I am reading the article on How to: Pass values between ASP.NET pages

In the second approach, they suggest hooking up a button and directing the user to another page using POST. I don't know how to do this. How do I HTTP POST?

"When the source page uses the HTTP POST action to navigate to the target page, you can retrieve posted values from the Form collection in the target page."

This is how I am sending the user to the new page:

    protected void btnSubmitForPost_Click(object sender, EventArgs e)
    {
        Response.Redirect("GetViaPost.aspx");
    }

EDIT The final solution: You can use ASP.NET webforms. Do the following: On the first page, create your controls and a button that sends the user to a new page. Handle the click event from this button. As stated below, use Server.Transfer with endResponse=false, instead of Response.Redirect(). When you use Response.Redirect, your post data is cleared out. I did not need to specify action in the form or anything else.

A: 

In ASP.NET when you click a button, you're posting the entire page's fields by default (as it's contained within a gigantic <form /> tag last time I checked. You can access these values after clicking the button like this:

string MyPostedValue = Request.Form["MyFormField"];

*Edit as per your update in your question, change Response.Redirect() to Server.Transfer() like this:

protected void btnSubmitForPost_Click(object sender, EventArgs e)
{
    Server.Transfer("GetViaPost.aspx", true);
}

Then in your GetViaPost.aspx's page you can get any form/query string variable you passed from your sending page like this:

string MyPostedValue = Request.Form["MyFormField"];
Mr. Smith
so how do I get the button to get the browser to go to a new page?
MedicineMan
Using either Response.Redirect or Server.Transfer.
Yuriy Faktorovich
You can try using Server.Transfer("mypage.aspx") in the button's click event. A button's default behavior is to post to the same page you're working on. Using Server.Transfer will persist the values from one page to another as far as I can remember.
Mr. Smith
Updated my answer as per your edit on your question. Hope it helps!
Mr. Smith
A: 

ASP.NET buttons always perform a POST. You can set which page the button posts to using the PostBackUrl property of the button. If you leave this blank, the button will post back to the same page that is resides on.

Check out this article for more information.

Shea Daniels
how? do I still set up an event handler?
MedicineMan
I edited my answer to provide a bit more detail. If you post to another page then no, you don't create an event handler for the button handler. You just use the page load event.
Shea Daniels
+1  A: 

If in the codebehind you wire up to the button click event, then click the button. It's a POSTback that happens.

Any controls that you have runat="server" will be accessible by their id (and any values set on them) in the codebehind.

In terms of posting data to other pages, you have a number of options available to you.

The querystring, sessions, cookies and viewstate.

A basic example (with no error handling) given your updated Response.Redirect might be:

int someId = int.Parse(txtBoxOnThePage.Text);
Response.Redirect(string.Format("GetViaPost.aspx?myId={0}", someId));

Then on the GetViaPost page you could pull that out by:

HttpContext.Current.Request.QueryString["myId"]

http://www.asp.net/learn/ is a surprisingly good source of information and tutorials for this kind of learning.

TreeUK
+1  A: 

If I'm reading this right, all of these answers are missing the question...

You're looking at posting from one Asp.Net form to another, and one of the methods is what you want to figure out - doing a normal http post. The book or article probably is already telling you about the Server.Transfer as another option if I'm guessing right.

If I'm getting the question right, then the simplest answer is to not use a standard ASP.Net form (with the runat = server attribute) as the starting point, but to use a simple standard html form to post to an asp.net page

<form action = "targetpage.aspx" method="post">
    ...some form fields here
   <input type = "submit">
</form>
David Stratton
If you're doing all your development in ASP.NET then this would require nesting forms. I'm guessing this would cause you trouble.
Shea Daniels
can this be done with a standard asp.net form? or is the HTTP POST with asp.net webform something that no one does?
MedicineMan
Agreed, if he wants a more back to basics way of working, he should check out ASP.NET MVC (which I love). Where your answer would be the default implementation. Otherwise he's missing the ease of webforms and all the handy server controls.
TreeUK
so if you are doing an HTTP POST, then you are probably using a simple standard html form instead of an webforms based one.
MedicineMan
It's not that it being an HTTP POST shows it's a standard HTML form. Webforms simply post to themselves by default, <form runat="server">. You can still have a webform post to another page as shown simply by adding the action="SomePage".
TreeUK
tried adding action="somepage" but I got: Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.
MedicineMan
This cannot be nested within a standard form runat="Server" AND this is a standard example that was in at least three books I've read about ASP.net.
David Stratton