如何从ASP.NET中的codebehind文件访问IFRAME? [英] How can I access an IFRAME from the codebehind file in ASP.NET?
问题描述
我试图从code隐藏aspx.cs文件中的IFRAME HTML控件设置属性。
我碰上了一个帖子,说你可以使用的FindControl找到非使用-Asp控制:
ASPX文件包括:
< IFRAME ID =contentPanel1=服务器/>
然后code-隐藏文件包括:
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
HTMLControl时contentPanel1 =(HTMLControl时)this.FindControl(contentPanel1);
如果(contentPanel1!= NULL)
contentPanel1.Attributes [SRC] =http://www.stackoverflow.com;}
除了它没有找到控制,contentPanel1为null。
更新1
看着呈现的HTML:
< IFRAME ID =ctl00_ContentPlaceHolder1_contentPanel1>< / IFRAME>
我试图改变code-背后:
HTMLControl时contentPanel1 =(HTMLControl时)this.FindControl(ctl00_ContentPlaceHolder1_contentPanel1);如果(contentPanel1!= NULL)
contentPanel1.Attributes [SRC] =http://www.clis.com;
但它并没有帮助。
我使用的是母版。
更新2
ASPX文件更改为:
< IFRAME ID =contentPanel1NAME =contentPanel1=服务器/>
也没有帮助
答
答案是显而易见的,不配甚至要求原来的问题。如果您有ASPX code:
< IFRAME ID =contentPanel1=服务器/>
和希望从code-隐藏文件访问iframe中,你只需访问:
this.contentPanel1.Attributes [SRC] =http://www.stackoverflow.com;
如果iframe的直接其中code上的页面上,你应该能够直接引用它:
contentPanel1.Attribute =价值;
如果不是(这是在孩子的控制,或母版),你需要的页面层次结构的一个好主意......或者使用编写的FindControl的递归版本(的穷举法)。
I am trying to set attributes for an IFRAME html control from the code-behind aspx.cs file.
I came across a post that says you can use FindControl to find the non-asp controls using:
The aspx file contains:
<iframe id="contentPanel1" runat="server" />
and then the code-behind file contains:
protected void Page_Load(object sender, EventArgs e)
{
HtmlControl contentPanel1 = (HtmlControl)this.FindControl("contentPanel1");
if (contentPanel1 != null)
contentPanel1.Attributes["src"] = "http://www.stackoverflow.com";
}
Except that it's not finding the control, contentPanel1 is null.
Update 1
Looking at the rendered html:
<iframe id="ctl00_ContentPlaceHolder1_contentPanel1"></iframe>
i tried changing the code-behind to:
HtmlControl contentPanel1 = (HtmlControl)this.FindControl("ctl00_ContentPlaceHolder1_contentPanel1");
if (contentPanel1 != null)
contentPanel1.Attributes["src"] = "http://www.clis.com";
But it didn't help.
i am using a MasterPage.
Update 2
Changing the aspx file to:
<iframe id="contentPanel1" name="contentPanel1" runat="server" />
also didn't help
Answer
The answer is obvious, and unworthy of even asking the original question. If you have the aspx code:
<iframe id="contentPanel1" runat="server" />
and want to access the iframe from the code-behind file, you just access it:
this.contentPanel1.Attributes["src"] = "http://www.stackoverflow.com";
If the iframe is directly on the page where the code is running, you should be able to reference it directly:
contentPanel1.Attribute = value;
If not (it's in a child control, or the MasterPage), you'll need a good idea of the hierarchy of the page... Or use the brute-force method of writing a recursive version of FindControl().
这篇关于如何从ASP.NET中的codebehind文件访问IFRAME?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!