asp.net 中将表单提交到另一页 Code-Behind(代码和html在不同的页面)

 更新时间:2009年04月13日 11:34:07   作者:  
To send Server control values from a different Web Forms page
The following shows a complete example of the code-behind file associated with the Web Forms page sending the information. Depending on whether you use Visual Basic or C#, make sure this sample is called Firstpage.aspx.vb or Firstpage.aspx.cs, respectively.
[Visual Basic]
Imports System
Public Class FirstPageClass :
Inherits System.Web.UI.Page
Protected first As System.Web.UI.WebControls.TextBox
Protected last As System.Web.UI.WebControls.TextBox
Protected Button1 As System.Web.UI.WebControls.Button
Public ReadOnly Property FirstName() As String
Get
' first is the name of a TextBox control.
Return first.Text
End Get
End Property
Public ReadOnly Property LastName() As String
Get
' last is the name of a TextBox control.
Return last.Text
End Get
End Property
Sub ButtonClicked(sender As Object, e As EventArgs)
Server.Transfer("secondpage.aspx")
End Sub
End Class
[C#]
using System;
public class FirstPageClass : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox first;
protected System.Web.UI.WebControls.TextBox last;
protected System.Web.UI.WebControls.Button Button1;
public string FirstName
{
get
{
return first.Text;
}
}
public string LastName
{
get
{
return last.Text;
}
}
void ButtonClicked(object sender, EventArgs e)
{
Server.Transfer("secondpage.aspx");
}
}
The following shows a complete example of how to create a Web Forms page with a code-behind file to pass the values of two TextBox controls to another Web Forms page. Make sure this sample is called Firstpage.aspx.
[Visual Basic]
<%@ Page Language="VB" Inherits="FirstPageClass" %>
<html>
<head>
</head>
<body>
<form runat="server">
First Name:
<asp:TextBox id="first"
runat="server"/>
<br>
Last Name:
<asp:TextBox id="last"
runat="server"/>
<br>
<asp:Button
id="Button1"
OnClick="ButtonClicked"
Text="Go to second page"
runat=server />
</form>
</body>
</html>
[C#]
<%@ Page Language="C#" Inherits="FirstPageClass" %>
<html>
<head>
</head>
<body>
<form runat="server">
First Name:
<asp:TextBox id="first"
runat="server"/>
<br>
Last Name:
<asp:TextBox id="last"
runat="server"/>
<br>
<asp:Button
id="Button1"
OnClick="ButtonClicked"
Text="Go to second page"
runat=server />
</form>
</body>
</html>
To receive Server control values from a different Web Forms page
The following shows a complete example of the code-behind file associated with the Web Forms page receiving the information. Depending on whether you use Visual Basic or C#, make sure this sample is called Secondpage.aspx.vb or Secondpage.aspx.cs, respectively.
[Visual Basic]
Imports System
Public Class SecondPageClass
Inherits System.Web.UI.Page
Protected DisplayLabel As System.Web.UI.WebControls.Label
Public fp As FirstPageClass
Sub Page_Load()
If Not IsPostBack Then
fp = CType(Context.Handler, FirstPageClass)
End If
End Sub
End Class
[C#]
using System;
public class SecondPageClass : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label DisplayLabel;
public FirstPageClass fp;
void Page_Load()
{
if (!IsPostBack)
{
fp = (FirstPageClass) Context.Handler;
}
}
}
The following shows a complete example of how to create a Web Forms page with a code-behind file to receive the values passed from a different Web Forms page. Make sure this sample is called Secondpage.aspx
[Visual Basic]
<%@ Page Language="VB" Inherits="SecondPageClass" %>
<%@ Reference Page="firstpage.aspx" %>
<html>
<head>
</head>
<body>
<form runat="server">
Hello <%=fp.FirstName%> <%=fp.LastName%>
</form>
</body>
</html>
[C#]
<%@ Page Language="C#" Inherits="SecondPageClass" %>
<%@ Reference Page="firstpage.aspx" %>
<html>
<head>
</head>
<body>
<form runat="server">
Hello <%=fp.FirstName%> <%=fp.LastName%>
</form>
</body>
</html>

相关文章

  • 如何为asp.net core添加protobuf支持详解

    如何为asp.net core添加protobuf支持详解

    这篇文章主要给大家介绍了关于如何为asp.net core添加protobuf支持的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2018-02-02
  • Asp.Net中索引器的用法分析

    Asp.Net中索引器的用法分析

    这篇文章主要介绍了Asp.Net中索引器的用法,以实例形式详细分析了Asp.Net中索引器的定义、属性与具体使用方法,并附带说明了相关的注意事项,在asp.net项目开发中有不错的参考借鉴价值,需要的朋友可以参考下
    2014-11-11
  • asp.net 结合YUI 3.0小示例

    asp.net 结合YUI 3.0小示例

    公司最近做了个WEB项目,网上这方面的东西也很少的,没办法就自己摸索了。 用到了Ajax这一段时间研究了一下它的用法,故来解释一下。
    2009-11-11
  • .Net反向代理组件Yarp用法详解

    .Net反向代理组件Yarp用法详解

    本文详细讲解了.Net反向代理组件Yarp的用法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-09-09
  • .Net Core日志记录之第三方框架Serilog

    .Net Core日志记录之第三方框架Serilog

    这篇文章介绍了.Net Core日志记录之第三方框架Serilog,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • asp.net下定制日期输出格式的代码

    asp.net下定制日期输出格式的代码

    asp.net下定制日期输出格式的代码...
    2007-09-09
  • ASP.NET中Dictionary基本用法实例分析

    ASP.NET中Dictionary基本用法实例分析

    这篇文章主要介绍了ASP.NET中Dictionary基本用法,结合实例形式分析了Dictionary的基本功能、使用步骤与相关操作技巧,需要的朋友可以参考下
    2016-08-08
  • asp.net 脏字典过滤问题 用正则表达式来过滤脏数据

    asp.net 脏字典过滤问题 用正则表达式来过滤脏数据

    asp.net 脏字典过滤问题 用正则表达式来过滤脏数据
    2009-10-10
  • asp.net 定时间点执行任务的简易解决办法

    asp.net 定时间点执行任务的简易解决办法

    这里的定时间点执行任务,指的是每天的某个时间执行一项任务。
    2009-12-12
  • VS2015 搭建Asp.net core开发环境的方法

    VS2015 搭建Asp.net core开发环境的方法

    最近想在vs2015体验下.net core,折腾了两天终于把环境弄好了。下面这篇文章就给大家分享下我的搭建过程,有需要的朋友们可以参考学习,下面来一起看看吧。
    2016-12-12

最新评论