How can I access an IFRAME from the codebehind file in ASP.NET?

后端 未结 10 1670
隐瞒了意图╮
隐瞒了意图╮ 2020-12-14 07:09

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-as

相关标签:
10条回答
  • 2020-12-14 07:29

    Where is your iframe embedded?

    Having this code

    <body>
    
    <iframe id="iFrame1" runat="server"></iframe>
    
    <form id="form1" runat="server">
    
    <div>
          <iframe id="iFrame2" runat="server"></iframe>
    </div>
    </form>
    

    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:

    FindControl("iFrame2") as System.Web.UI.HtmlControls.HtmlGenericControl
    
    0 讨论(0)
  • 2020-12-14 07:29

    Try instantiating contentPanel1 outside the Load event; keep it global to the class.

    0 讨论(0)
  • 2020-12-14 07:30

    The FindControl method looks in the child controls of the "control" the method is executed on. Try looking through the control collection recursively.

        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;
        }
    
    0 讨论(0)
  • 2020-12-14 07:33

    Try using

    this.Master.FindControl("ContentId").FindControl("controlId")
    

    instead.

    0 讨论(0)
  • 2020-12-14 07:36

    None of your suggestions worked for me, here is my solution:

    add src="<%=_frame1%>" //to the iframe id="frame1" html control
    public string _frame1 = "http://www.google.com";
    
    0 讨论(0)
  • 2020-12-14 07:39
    <iframe id="yourIframe" clientIDMode="static" runat="server"></iframe>
    

    You should them be able to find your iframe using the findcontrol method.

    setting clientIDMode to Static prevents you object from being renamed while rendering.

    0 讨论(0)
提交回复
热议问题