简体   繁体   中英

ASP.NET to PHP conversion in web development

How do I pass information back and forth in an asp.net web application throught the html forms? I know how to do this in PHP, but I can't seem to think about it through the code-behind thing (i think that's what its called). (note: i only started looking at asp.net a few hours ago. i decided to try it by recreating a simple page)

You can post to an ASP.NET page with a standard HTML form like this:

<form action="/MyPage.aspx" method="post"> 
    <input type="text" name="name" />
</form>

Then in the code behind of MyPage.aspx, you can access the form elements like this:

protected void Page_Load(object sender, System.EventArgs e)
{
    string name = Request.Form["name"];
}

It should also be noted that most ASP.NET books would probably teach posting back to the same page. You can then access the form items on the page via the objects, then do a Response.Redirect() to the next page you want to go to.

In this case the aspx would look like this:

<asp:TextBox runat="server" id="Name" />

And you would access the value from the codebehind like this:

protected void Page_Load(object sender, System.EventArgs e)
{
    if(Page.IsPostBack)
    {
        string name = Name.Text;
    }
}

One of the biggest things to think about is the fact that you don't really have control of the form postbacks in classic ASP.NET WebForms. That was a big paradigm shift for me when I moved from PHP to ASP.NET. You do have one handy object, however, that can make things easier on you. In ASP.NET WebForms, you can access the Session object. You can store almost anything in the Session and it will be visible between forms. The only thing you need to be careful about is that sessions expire .

State-less

PHP fits the state-less Internet model very well. It is possible to write simple forms from scratch and have them do what you expect straight away. However ASP.NET bends the state-less model to make it similar to designing software for the desktop.

Therefore I presume you are trying to type out ASP.NET commands in notepad as you are used to in PHP but this will probably lead to frustration.

Wizards

To get some initial understanding of how forms are structured and built, maybe you should build a simple application using the wizards and tutorials provided in Visual Studio, then go under the hood and see what code it has produced.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM