您的位置:首页 > 其它

iFrame页面数据的绑定与读取

2009-03-11 10:20 225 查看
iframeStudy.aspx.cs
/***********iFrame页面数据的绑定与读取**********************
* 为iFrame加上runat=server的作法,没有尝试,
* 需要考虑window.onload与Page_Load的执行次序吧
*
* 用HiddenField做中介的方法,容易出错的地方:
* 1.window.onload事件执行的时候iframe一般没有加载完成,要判断
* 2.表单提交的时候,用js为HiddenField赋值onsubmit=bSubmit();
***********************************************************/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class iframeStudy : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
Bind();
}
private void Bind()
{
this.hfEdit.Value = "绑定数据";
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
Response.Write("<mce:script type="text/javascript"><!--
alert('" + this.hfEdit.Value + "');
// --></mce:script>");
}
}
iframeStudy.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="iframeStudy.aspx.cs" Inherits="iframeStudy" %>

<!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 runat="server">
<title>iFrame页面数据的绑定与读取</title>
<mce:script type="text/javascript"><!--
//为iFrame绑定数据
function bindIframe()
{
//判断iframe页面是否加载完成
if(window.frames[0].document.readyState=="complete")
{
window.childFrame.document.getElementById("txtEdit").value=document.getElementById("hfEdit").value;
}
else//如果没有加载完成,过一秒钟再执行
setTimeout("bindIframe()",1000);
}
//表单提交的时候,改变HiddenField的值,HiddenField相当于一个中介吧.
function bSubmit(){
document.getElementById("hfEdit").value=window.childFrame.document.getElementById("txtEdit").value;
}
window.onload=function(){
bindIframe();
}
function fShow(){
alert(document.getElementById("hfEdit").value);
}

// --></mce:script>
</head>
<body>
<form id="form1" runat="server" onsubmit="bSubmit();">
<div>
<asp:HiddenField ID="hfEdit" runat="server" />
</div>
<div>
<iframe runat="server" id="childFrame" width="100%" height="100%" src="childFrame.htm" mce_src="childFrame.htm" frameborder="0">
</iframe>
</div>
<input id="btnOk" type="button" value="Ok" onclick="fShow();" />
<asp:Button ID="btnSubmit" runat="server" Text="提交" OnClick="btnSubmit_Click" />
</form>
</body>
</html>
childFrame.htm
<!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>
<title>iFrame内的页面</title>
</head>
<body>
<textarea id="txtEdit" cols="20" rows="2">
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐