C#给iframe赋值的问题
我在网上看到一个用Attributes["src"]给iframe赋值的例子,为什么我测试不成功,请教下大家:
1.aspx的文件代码
<%@ Page Language="C#" %>
<html>
<head>
</head>
<body>
<form id="Form1" runat="server">
<!-- Insert content here -->
<iframe width="97%" height="580" id="ifraContent" name="ifraContent" runat=server ></iframe>
</form>
</body>
</html>
1.aspx.cs的代码
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ifraContent.Attributes["src"]="http://www.baidu.com";
}
}
为什么我运行后。只出现了iframe框框,却不能显示出百度的网站呢。有人能告诉我缺少了什么吗?
[解决办法]
如果你没有声明CodeBehinde,那么代码就应该写在aspx内。
例如:
<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
ifraContent.Attributes["src"] = "http://www.baidu.com";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<iframe width="97%" height="580" id="ifraContent" name="ifraContent" runat=server ></iframe>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<iframe width="97%" height="580" id="ifraContent" name="ifraContent" runat="server"></iframe>
</body>
</html>