日期:2014-05-18  浏览次数:20480 次

如何访问iframe中的成员
default.aspx中有个iframe(src= "webform1.aspx "),在webform1.aspx里有个隐藏域hid,在default.aspx的后台中如何访问该隐藏域?

------解决方案--------------------
下面是主要代码,楼主参考一下:

default.aspx:
<script language= "javascript ">
//在提交之前,获取iframe中的指定对象的值,然后将该值赋给页面中的隐藏域(这是HiddenField的实例,在后台处理事件中,获取此HiddenField的值)
function s()
{
var val = window.frames[ "hf "].document.getElementById( "h1 ").value;
document.getElementById( "hf1 ").value = val;

return true;
}
</script>
<!-- 省略html代码 -->
<!-- 这里是一个ifreame -->
<div>
<iframe width= "200 " height= "200 " id= "hf " src= "webform1.aspx ">
</iframe>
</div>
<asp:HiddenField ID= "hf1 " runat= "server " />
<asp:Button ID= "btn " runat= "server " OnClientClick= "return s() " OnClick= "Click " Text= "Click " />

default.aspx.cs:
//按钮事件代码
protected void Click(object sender, EventArgs e)
{
Response.Write(hf1.Value);
}

webform1.aspx:
<input type= "hidden " id= "h1 " value= "h1 " />
------解决方案--------------------
假如webform1.aspx隐藏域是hd.
要在default.aspx中访问iframe中的webform1.aspx中的控件,如果直接在default.aspx的后台来访问webform1.aspx是行不通的..
但是办法是人想出来的,我们可以首先通过js来获取Webform1.aspx中的hd的内容,然后我们在default.aspx中加入一个html textbox(hd_Text)控件并把他设置为runat=server,把js获得的value值赋给hd_Text,这样一来,我门就可以访问Webform1.aspx中的内容了..
具体代码如下:
default.aspx
<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> 无标题页 </title>
<script>
function GetValue()
{
document.getElementById( "hd_Text ").value=IFRAME1.document.getElementById( "hd ").value;
}
</script>
</head>
<body onload= "GetValue(); ">
<form id= "form1 " runat= "server ">
<div>
<iframe src=webform1.aspx id= "IFRAME1 "> </iframe>
<br />
<br />
<asp:Button ID= "Button1 " runat= "server " Text= "Button " OnClick= "Button1_Click " /> &nbsp; <input
id= "Button2 " type= "button " value= "button " onclick= "GetValue() " />
<input id= "hd_Text " type= "text " style= "display:none " runat=server /> </div>
</form>
</body>
</html>

webform1.aspx
<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> 无标题页 </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<asp:HiddenField ID= "hd " runat= "server " Value= "haha " />
</div>
</form>
</body>
</html>