如何动态更改iframe的src属性 [英] How to change src Attribute of iframe dynamically

查看:148
本文介绍了如何动态更改iframe的src属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从文件后面的代码修改或添加iframe的"src"属性,但无法这样做.
.aspx:-

I am trying to modify or add the "src" attribute of the an Iframe from code behind file but unable to do so.
.aspx:-

<iframe id="Iframe1">
</iframe>


.cs:-


.cs:-

HtmlLink link1 = (HtmlLink)Page.FindControl("Iframe1");
link1.Attributes.Add("src", "http://www.xyz.com");


我收到的错误:


The error I am getting:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

推荐答案

您的代码有误,请尝试这样,
Your code is wrong, try like this,
HtmlControl frame1 = (HtmlControl)this.FindControl("Iframe1");
frame1.Attributes["src"] = "http://www.xyz.com";



顺便说一句从代码隐藏-ASP.NET动态加载IFRAME中的页面 [ ^ ]



BTW check this Loading pages in IFRAME dynamically from codebehind - ASP.NET[^]


HtmlControl x = (HtmlControl)this.frame1;
           

x.Attributes.Add("src", "http://www.xyz.com"])


这篇关于如何动态更改iframe的src属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆