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";
Try this.
ContentPlaceHolder cplHolder = (ContentPlaceHolder)this.CurrentMaster.FindControl("contentMain");
HtmlControl cpanel= (HtmlControl)cplHolder.FindControl("contentPanel1");
The FindControl method looks in the child controls of the "control" the method is executed on. Try looking through the control collection recursively.
This works for me.
ASPX :
I can access the iframe directly via id.
Code Behind :
Where is your iframe embedded?
Having this code
I can access with
iFrame1.Attributes["src"]
just to iFrame1 and not to iFrame2.Alternatively, you can access to any element in your form with: