简体   繁体   English

在Web开发中将ASP.NET转换为PHP

[英]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? 如何通过html表单在asp.net Web应用程序中来回传递信息? 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). 我知道如何在PHP中执行此操作,但是我似乎无法通过代码隐藏的东西来思考它(我认为这就是所谓的含义)。 (note: i only started looking at asp.net a few hours ago. i decided to try it by recreating a simple page) (注意:我只是几个小时前才开始查看asp.net。我决定通过重新创建一个简单页面来尝试使用它)

You can post to an ASP.NET page with a standard HTML form like this: 您可以使用以下标准HTML表单发布到ASP.NET页面:

<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: 然后,在MyPage.aspx后面的代码中,您可以像这样访问表单元素:

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. 还应该注意的是,大多数ASP.NET书籍都可能教导将内容发回到同一页面。 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. 然后,您可以通过对象访问页面上的表单项,然后对要转到的下一页执行Response.Redirect()。

In this case the aspx would look like this: 在这种情况下,aspx如下所示:

<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. 要考虑的最大的事情之一是,您实际上没有控制经典ASP.NET WebForms中的表单回发的事实。 That was a big paradigm shift for me when I moved from PHP to ASP.NET. 当我从PHP转到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. 在ASP.NET WebForms中,您可以访问Session对象。 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. PHP非常适合无状态Internet模型。 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. 但是,ASP.NET弯曲了无状态模型,使其类似于为桌面设计软件。

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. 因此,我假设您正尝试像在PHP中那样在记事本中键入ASP.NET命令,但这可能会导致沮丧。

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. 为了初步了解表单的结构和构建方式,也许您应该使用Visual Studio中提供的向导和教程来构建一个简单的应用程序,然后深入研究其生成的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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