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)
如何通过html表单在asp.net Web应用程序中来回传递信息?我知道如何在PHP中执行此操作,但我似乎无法通过代码隐藏的事情来考虑它(我认为这就是它所谓的)。 (注意:几小时前我才开始关注asp.net。我决定通过重新创建一个简单的页面来尝试它)
3 个解决方案
#1
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. 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.
还应该注意的是,大多数ASP.NET书籍可能会教导回发到同一页面。然后,您可以通过对象访问页面上的表单项,然后执行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;
}
}
#2
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.
要考虑的最重要的事情之一就是你不能真正控制经典ASP.NET WebForms中的表单回发。当我从PHP迁移到ASP.NET时,这对我来说是一个很大的范例转换。但是,你确实有一个方便的对象可以让你的事情变得更轻松。在ASP.NET WebForms中,您可以访问Session对象。您可以在会话中存储几乎任何内容,并且它将在表单之间可见。您唯一需要注意的是会话到期。
#3
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.
PHP非常适合无状态Internet模型。可以从头开始编写简单的表单,让它们直接执行您期望的操作。然而,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命令,因为您习惯于使用PHP,但这可能会导致挫败感。
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中提供的向导和教程构建一个简单的应用程序,然后深入了解它生成的代码。
#1
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. 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.
还应该注意的是,大多数ASP.NET书籍可能会教导回发到同一页面。然后,您可以通过对象访问页面上的表单项,然后执行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;
}
}
#2
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.
要考虑的最重要的事情之一就是你不能真正控制经典ASP.NET WebForms中的表单回发。当我从PHP迁移到ASP.NET时,这对我来说是一个很大的范例转换。但是,你确实有一个方便的对象可以让你的事情变得更轻松。在ASP.NET WebForms中,您可以访问Session对象。您可以在会话中存储几乎任何内容,并且它将在表单之间可见。您唯一需要注意的是会话到期。
#3
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.
PHP非常适合无状态Internet模型。可以从头开始编写简单的表单,让它们直接执行您期望的操作。然而,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命令,因为您习惯于使用PHP,但这可能会导致挫败感。
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中提供的向导和教程构建一个简单的应用程序,然后深入了解它生成的代码。