How do I perform an HTTP POST using an ASP.NET button?

Sorry, another super basic ASP.NET question. it's so embarrassing.

I am reading an article on How To: Pass Values โ€‹โ€‹Between ASP.NET Pages

In the second approach, they suggest connecting a button and directing the user to another page using a POST. I do not know how to do that. How do I perform an HTTP POST?

"When the source page uses the HTTP POST action to navigate to the landing page, you can retrieve the posted values โ€‹โ€‹from the Form collection on the landing page.

This is how I send the user to a new page:

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

      

EDIT Final Solution: You can use ASP.NET Web Forms. Follow these steps: On the first page, create controls and a button that will send 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 data will be deleted. I didn't need to specify the action on the form or whatever.

+2


source to share


4 answers


If I read this right, there is no question for all these answers ...

You are looking at submitting from one Asp.Net form to another, and one of the methods is what you want to understand - a regular mailing. Perhaps a book or article already tells you about Server.Transfer as another option, if I assume that's correct.



If I am asking the question correctly, the simplest answer would be not to use a standard ASP.Net form (with runat = server attribute) as a 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>

      

+1


source


In ASP.NET, when you click a button, you publish by default all the page fields (as they were contained in the gigantic tag <form />

that I last checked. You can access these values โ€‹โ€‹after clicking a 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 on the GetViaPost.aspx page, you can get any form / request string submitted from your submit page like this:

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

      

+2


source


If in your code-behind you connect to a button click event, click the button. This is the POSTback that is happening.

Any controls you have runat="server"

will be accessible by their ID (and any values โ€‹โ€‹set on them) in your code.

When it comes to placing data on other pages, you have several options available to you.

Request, sessions, cookies and viewstate.

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

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

      

Then on the GetViaPost page you can get this:

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

      

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

+1


source


ASP.NET buttons always POST. You can specify which page the button is on using the button's PostBackUrl property. If you leave this blank, the button will return to the same page it is on.

See this article for more information .

0


source







All Articles