rory
rory

Reputation: 1438

Asp.Net Control.ClientID returns object instead of string

Sometimes in asp.net, the Control.ClientID returns the whole Control object instead of the ClientID string. I haven't paid too much attention to it up until now as I could just fish it out client side with ClientID.Id. But now I need to pass the ID of an iframe and IE won't let me pass the iframe object so I have to get the string of the ID this time.

The iframe:

<iframe ID="VideoFrame" runat="server" class="embed-responsive-item" frameborder="0" allowfullscreen src="https://www.youtube.com/embed/" ></iframe>

<asp:Literal runat="server" ID="YouTubeScript" />

The Codebehind:

YouTubeScript.Text = @"
            <script type='text/javascript'>
                $(function() {
                    " + Helpers.CallJavascriptFunction("InitYoutubeVideo", VideoFrame.ClientID) + @"
                });
            </script>
        ";

Update/Answer I need to wrap VideoFrame.ClientID in quotation marks so that javascript/jquery recognises it as a string. Otherwise it sees the id and converts it into the html object belonging to that id.

Upvotes: 1

Views: 526

Answers (2)

Steve Harris
Steve Harris

Reputation: 5109

You have two options. Use the client ID properly:

<script type='text/javascript'>
    $(document).ready(function() {
        InitYoutubeVideo('<%=VideoFrame.ClientID%>');
    });
</script>

Or make the client id static:

<iframe ID="VideoFrame" runat="server" ClientIdMode="static" class="embed-responsive-item" frameborder="0" allowfullscreen src="https://www.youtube.com/embed/" ></iframe>

<script type='text/javascript'>
    $(document).ready(function() {
        InitYoutubeVideo('VideoFrame');
    });
</script>    

Upvotes: 0

VDWWD
VDWWD

Reputation: 35544

It looks like you are trying to mix javascript and inline aspnet code somehow. it should look like this:

<script type="text/javascript">
    Helpers.CallJavascriptFunction("InitYoutubeVideo", "<%= VideoFrame.ClientID %>");
</script>

Upvotes: 1

Related Questions