locked
System.NullReferenceException - access iframe by Id RRS feed

  • Question

  • User-909867351 posted

    Hi I want to access one iframe ane put his src to a youtube video. Here is my code:

     protected void Mostra_Filme(object sender, EventArgs e)
        {
            ImageButton btn = sender as ImageButton;
            //Get the listviewitem from button
            RepeaterItem item = (RepeaterItem)(sender as Control).NamingContainer;
            //Find the label control
            Label filme = (Label)item.FindControl("video");
            HtmlControl frame1 = (HtmlControl)this.FindControl("Iframe1");
            frame1.Attributes["src"] = "https://www.youtube.com/watch?v=w1-LoH0iKWU";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "openModal();", true);
        }

    and aspx file

    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog modal-lg" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="exampleModalLabel">Filme Promocional</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <div class="row justify-content-center">
                            <div class="col-md-10 ">
                                <iframe  width="560" height="315"  id="Iframe1"  frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
                            </div>                                               
                        </div>                                              
                    </div>            
          <div class="modal-footer">        
              <asp:Button ID="Button2" CssClass="btn btn-primary" runat="server" Text="Confirmar"  />
              <br />                  
          </div>
        </div>
      </div>
    </div>

    Why do I get this error?

    Thank you

    Wednesday, October 23, 2019 6:09 PM

Answers

  • User475983607 posted

    The iframe needs a runat="server" attribute in order to see the iframe in the code behind.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 23, 2019 6:15 PM