一直以来对Frame都不是太理解。前两天无事可做,在博客园逛了一趟。看到一位朋友‘Fly With My Heart’ 写的关于Frame页面传值的文章,很是感兴趣。但把他提供的少许代码在VS2005运行后,却不成功。之后我改了代码,还是不行,可能是我比较苯吧。再之后就是Baidu以下,可还是不行。昨天上课问了下老师,终于搞明白啦。现在把代码共享以下,以方便那些需要的朋友。我这次用的是VS2003 写的代码,有意向的朋友可以把它翻译成05的。
1、Default.aspx 页面
<%@ Page Language="c#" AutoEventWireup="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Frame页面传值</title>
</head>
<frameset rows="*" cols="300,*" id="myFrame">
<frame name="left" src="left.aspx" >
<frame name="right"src="right.aspx">
</frameset>
<noframes>
<body></body>
</noframes>
</html>
2、left.aspx页面
<%@ Page Language="c#" Inherits="TestFrame.left" CodeFile="left.aspx.cs" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML>
<HEAD>
<title>leftFrameset</title>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
<BODY>
</SCRIPT>
<form runat="server">
<p>
<asp:TextBox ID="TextBox1" runat="server" Width="224px" Height="40px" />
</p>
<p>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Width="120px" Height="32px"
Text="Frame页面传值" />
</p>
</form>
</BODY>
</HTML>
3、left.aspx.cs页面
public void Button1_Click(object sender, System.EventArgs e)
{
string test = TextBox1.Text;
Page.RegisterStartupScript("kkk", "<script language='javascript'>window.open('right.aspx?Param=" + test + "','right')</script>");
}
4、right.aspx页面
<%@ Page Language="c#" ContentType="text/html" ResponseEncoding="gb2312" Inherits="frameset.right" CodeFile="right.aspx.cs" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML>
<HEAD>
<title>rightFrameset</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
<body>
<form runat="server">
<asp:Label ID="Label1" runat="server">
Frame页面传的值</asp:Label>
</form>
</body>
</HTML>
5、right.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 frameset
{
/// <summary>
/// right 的摘要说明。
/// </summary>
public partial class right : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
if(Request.Params["Param"]!=null)
{
Label1.Text = Request.Params["Param"].ToString();
}
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
}
#endregion
}
}
以上就是我的代码,如有不足之处,还请各位指出,谢谢。
希望和各位大虾们成为挚友。