如何在两个页面传递数据?

qinda 2003-06-11 06:15:24
我去找了个例程
<%@ Page language="c#" Codebehind="firstpage.aspx.cs" AutoEventWireup="false" ClassName="FirstPageClass" Inherits="test.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<script runat="server">
public string FirstName
{
get
{
return first.Text;
}
}
public string LastName
{
get
{
return last.Text;
}
}
void ButtonClicked(object sender, EventArgs e)
{
Server.Transfer("secondpage.aspx");
}
</script>
</HEAD>
<body>
<form id="Form1" runat="server">
First Name:
<asp:textbox id="first" runat="server"></asp:textbox><br>
Last Name:
<asp:textbox id="last" runat="server"></asp:textbox><br>
<asp:button id="Button1" onclick="ButtonClicked" runat="server" Text="Go to second page"></asp:button></form>
</body>
</HTML>
×××××××××××××××××××××××××××××××××××××
<%@ Page language="c#" Codebehind="secondpage.aspx.cs" Debug="True" AutoEventWireup="false" Inherits="test.secondpage" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<%@ Reference Page="firstpage.aspx" %>
<html>
<head>
<script runat="server">
FirstPageClass fp;
void Page_Load()
{
if (!IsPostBack)
{
fp = (FirstPageClass)Context.Handler;
}
}
</script>
</head>
<body>
<form runat="server" ID="Form1">
Hello <%=fp.FirstName%> <%=fp.LastName%>
</form>
</body>
</html>
firstpage里的Textbox的值应该传给secondpage,但是我运行的时候总提示
Object reference not set to an instance of an object.
为什么?我该怎么改呀?
...全文
35 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
babytong 2003-06-12
  • 打赏
  • 举报
回复
Firstpage.aspx
<%@ Page language="c#" Codebehind="FirstPage.aspx.cs" AutoEventWireup="false" Inherits="WebProject1.FirstPage" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="WebForm1" method="post" runat="server">
<FONT face="宋体">
<asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 390px; POSITION: absolute; TOP: 164px" runat="server"></asp:TextBox>
<asp:TextBox id="TextBox2" style="Z-INDEX: 102; LEFT: 391px; POSITION: absolute; TOP: 212px" runat="server"></asp:TextBox>
<asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 407px; POSITION: absolute; TOP: 274px" runat="server" Text="Button"></asp:Button></FONT>
</form>
</body>
</HTML>
fristpage.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebProject1
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class FirstPage : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}


public string FirstName
{
get
{
return TextBox1.Text;
}
}
public string LastName
{
get
{
return TextBox2.Text;
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
Server.Transfer("secondpage.aspx");
}
}
}
secondpage.aspx

<%@ Page language="c#" Codebehind="secondpage.aspx.cs" AutoEventWireup="false" Inherits="WebProject1.WebForm1" %>
<%@ Reference Page="firstpage.aspx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="WebForm1" method="post" runat="server">
<FONT face="宋体">
<asp:Label id="Label1" style="Z-INDEX: 101; LEFT: 322px; POSITION: absolute; TOP: 160px" runat="server">Label</asp:Label>
<asp:Label id="Label2" style="Z-INDEX: 102; LEFT: 323px; POSITION: absolute; TOP: 209px" runat="server">Label</asp:Label></FONT>
</form>
</body>
</HTML>

------
secondpage.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebProject1
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
public FirstPage fp;
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
fp = (FirstPage)Context.Handler;
}
Label1.Text =fp.FirstName;
Label2.Text = fp.LastName;
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}
babytong 2003-06-12
  • 打赏
  • 举报
回复
你错在(FirstPageClass)Context.Handler;
你的第一个页面的类名称是叫FirstPageClass吗?怕不是吧
babytong 2003-06-12
  • 打赏
  • 举报
回复
那个没问题,我给你一个例子
qinda 2003-06-12
  • 打赏
  • 举报
回复
janssenkm,coolnick你们给一个用session传的例子好不好?
coolnick 2003-06-12
  • 打赏
  • 举报
回复
同意楼上,用 SESSION传也行
飞行石 2003-06-12
  • 打赏
  • 举报
回复
固然好用,但是不如URL后面跟参数实用,
qinda 2003-06-12
  • 打赏
  • 举报
回复
没看出来跟我的例程有什么区别啊?
llitcwl 2003-06-12
  • 打赏
  • 举报
回复
但是我发现在测试时,session经常会无故失效或改变呀?(在IE中就好了)
xz8000 2003-06-12
  • 打赏
  • 举报
回复
SESSION传最简单:

1:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Session("Str1")="A"
Session("Str2")="B"
End Sub

2:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dim Str1 as String
dim Str2 as String

Str1=Session("Str1")
Str2=Session("Str2")

End Sub
comy 2003-06-11
  • 打赏
  • 举报
回复
下面显示一个完整的 Web 窗体页,该页从另一个 Web 窗体页接收两个值。然后,这些值显示在 Web 窗体页上。您必须将该示例叫做 secondpage.aspx。

[Visual Basic]
<%@ Page Language="VB" %>
<%@ Reference Page="firstpage.aspx" %>

<html>

<head>

<script runat="server">

Dim fp As FirstPageClass

Sub Page_Load()
If Not IsPostBack Then
fp = CType(Context.Handler, FirstPageClass)
End If
End Sub

</script>

</head>

<body>

<form runat="server">

Hello <%=fp.FirstName%> <%=fp.LastName%>

</form>

</body>

</html>
[C#]
<%@ Page Language="C#" %>
<%@ Reference Page="firstpage.aspx" %>

<html>

<head>

<script runat="server">

FirstPageClass fp;

void Page_Load()
{
if (!IsPostBack)
{
fp = (FirstPageClass)Context.Handler;
}
}

</script>

</head>

<body>

<form runat="server">

Hello <%=fp.FirstName%> <%=fp.LastName%>

</form>

</body>
</html>
comy 2003-06-11
  • 打赏
  • 举报
回复
下面是一个完整的示例,显示如何使用内联代码创建 Web 窗体页以将两个 TextBox 控件的值传递到另一个 Web 窗体页。该示例的名称必须是 firstpage.aspx。

[Visual Basic]
<%@ Page Language="VB" ClassName="FirstPageClass" %>

<html>
<head>

<script runat="server">
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

</script>

</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
OnClick="ButtonClicked"
Text="Go to second page"
runat=server />
</form>

</body>

</html>
[C#]
<%@ Page Language="C#" ClassName="FirstPageClass" %>

<html>
<head>

<script runat="server">

public string FirstName
{
get
{
return first.Text;
}
}

public string LastName
{
get
{
return last.Text;
}
}

void ButtonClicked(object sender, EventArgs e)
{
Server.Transfer("secondpage.aspx");
}

</script>

</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
OnClick="ButtonClicked"
Text="Go to second page"
runat=server />
</form>

</body>

</html>

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧