asp.net의 CodeBehind 파일에서 iframe에 액세스하려면 어떻게해야합니까?
문제
Code-Behind Aspx.cs 파일에서 iframe html 컨트롤 속성을 설정하려고합니다.
나는 만났다 게시하다 즉, FindControl을 사용하여 다음을 사용하여 Nonasp 컨트롤을 찾을 수 있습니다.
ASPX 파일에는 다음이 포함됩니다.
<iframe id="contentPanel1" runat="server" />
그런 다음 코드-비만 파일에는 다음이 포함됩니다.
protected void Page_Load(object sender, EventArgs e)
{
HtmlControl contentPanel1 = (HtmlControl)this.FindControl("contentPanel1");
if (contentPanel1 != null)
contentPanel1.Attributes["src"] = "http://www.stackoverflow.com";
}
컨트롤을 찾지 못한다면 ContentPanel1은 NULL입니다.
업데이트 1
렌더링 된 HTML을보고 :
<iframe id="ctl00_ContentPlaceHolder1_contentPanel1"></iframe>
코드-홀드를 다음으로 변경하려고 시도했습니다.
HtmlControl contentPanel1 = (HtmlControl)this.FindControl("ctl00_ContentPlaceHolder1_contentPanel1");
if (contentPanel1 != null)
contentPanel1.Attributes["src"] = "http://www.clis.com";
그러나 그것은 도움이되지 않았습니다.
나는 마스터 페이지를 사용하고 있습니다.
업데이트 2
ASPX 파일 변경 :
<iframe id="contentPanel1" name="contentPanel1" runat="server" />
또한 도움이되지 않았습니다
대답
대답은 분명하며 원래의 질문을하는 것도 가치가 없습니다. ASPX 코드가있는 경우 :
<iframe id="contentPanel1" runat="server" />
Code-Behind 파일에서 iframe에 액세스하려면 다음과 같이 액세스 할 수 있습니다.
this.contentPanel1.Attributes["src"] = "http://www.stackoverflow.com";
해결책
코드가 실행중인 페이지에 iframe이 직접 있으면 직접 참조 할 수 있어야합니다.
contentPanel1.Attribute = value;
그렇지 않은 경우 (어린이 통제 또는 마스터 페이지에 있으면) 페이지의 계층 구조에 대한 좋은 아이디어가 필요합니다.
다른 팁
이것은 나를 위해 작동합니다.
ASPX :
<iframe id="ContentIframe" runat="server"></iframe>
ID를 통해 직접 iframe에 액세스 할 수 있습니다.
뒤에 코드 :
ContentIframe.Attributes["src"] = "stackoverflow.com";
당신의 iframe은 어디에 포함되어 있습니까?
이 코드가 있습니다
<body>
<iframe id="iFrame1" runat="server"></iframe>
<form id="form1" runat="server">
<div>
<iframe id="iFrame2" runat="server"></iframe>
</div>
</form>
나는 접근 할 수있다 iFrame1.Attributes["src"]
iframe1에 그리고 iframe2가 아닙니다.
또는 다음과 같이 양식의 모든 요소에 액세스 할 수 있습니다.
FindControl("iFrame2") as System.Web.UI.HtmlControls.HtmlGenericControl
사용해보십시오
this.Master.FindControl("ContentId").FindControl("controlId")
대신에.
로드 이벤트 밖에서 ContentPanel1을 인스턴스화 해보십시오. 수업에 전 세계적으로 유지하십시오.
FindControl 메소드는 메소드가 실행되는 "제어"의 어린이 컨트롤을 살펴 봅니다. 제어 컬렉션을 다시 살펴보십시오.
protected virtual Control FindControlRecursive(Control root, String id)
{
if (root.ID == id) { return root; }
foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
{
return t;
}
}
return null;
}
이 시도.
ContentPlaceHolder Cplholder = (ContentPlaceHolder) this.CurrentMaster.FindControl ( "ContentMain");
htmlcontrol cpanel = (htmlcontrol) cplholder.findcontrol ( "contentpanel1");
<iframe id="yourIframe" clientIDMode="static" runat="server"></iframe>
FindControl 메소드를 사용하여 iframe을 찾을 수 있어야합니다.
환경 clientIDMode
에게 Static
렌더링하는 동안 개체의 이름이 바뀌지 않도록합니다.
귀하의 제안 중 어느 것도 저를 위해 작동하지 않았습니다. 여기 내 해결책이 있습니다.
add src="<%=_frame1%>" //to the iframe id="frame1" html control
public string _frame1 = "http://www.google.com";
ASPX 페이지
<iframe id="fblikes" runat="server"></iframe>
뒤에 코드
this.fblikes.attributes [ "src"] = "/productdetails/fblike.ashx";
아주 간단합니다 ....