Skip to main content

Microsoft Silverlight

Answered Question Get silverlight html containerRSS Feed

(0)

frantisekfelt
frantise...

Member

Member

0 points

3 Posts

Get silverlight html container

Hi,

is there any possible way to get id of silverlight html container?

 

In my aspx page i have: 

 

1    <asp:Content ID="Content2" ContentPlaceHolderID="CphMainContent" runat="server">
2    <asp:Silverlight ID="Silverlight1" runat="server" Source="Silverlight/Komix.RSP.Application.Designer.xap" MinimumVersion="3.0.40307.0" Width="1280" Height="768" />
3    </asp:Content> 
 
But in final html source code is id of silverlight element something like 'ctl00_CphMainContent_Silverlight1_parent'.
And in my silverlight app i need to get dynamically id of this element.
Can someone help me? 

pbrooks
pbrooks

Contributor

Contributor

2671 points

355 Posts

Silverlight MVP

Re: Get silverlight html container

Personally, I would recommend using the object tag to render your Silverlight plugin instead of the server-side ASP.NET tag.  You have more control this way. If you must use the ASP.NET server-side tag, you could query the object tag using jQuery.  Something like this would probably suffice:

....
<div id="silverlightControlHost">
  <object data="data:application/x-silverlight-2," ....
....

 <script type="text/javascript">

  $(function() {

      var slControl = $("silverlightControlHost > object");

  });

</script>

 Or if you just wanted the surrounding div, you could do something like this:

  <script type="text/javascript">

  $(function() {

      var slContainer = $("silverlightControlHost");

  });

</script>

If this has answered your question, please click on "Mark as Answer" on this post.

Thanks,
Page Brooks
Silverlight MVP, MCSD
PageBrooks.com | @pbrooks

frantisekfelt
frantise...

Member

Member

0 points

3 Posts

Re: Re: Get silverlight html container

Thanks for suggestion, but i realy need to get id of silverlight html container in SL code.

pbrooks
pbrooks

Contributor

Contributor

2671 points

355 Posts

Silverlight MVP
Answered Question

Re: Re: Get silverlight html container

If you only have one object tag on your page, you could use the following jQuery code to obtain the id of the html container and then store it off in a hidden field:

Html:

<input type="hidden" id="hostId"  />

JS:
var hostId = $("object").parent().attr('id');
$("#hostId").val(hostId);

Then, if you need this value in your SL app, you could then use HTML DOM interop to retrieve the value.

Silverlight C#:

string hostId = HtmlPage.Document.GetElementById("hostId").GetAttribute("value");

If you have more than one object tag, you would simply need to modify the jQuery selector code to pick the one you need.

If this has answered your question, please click on "Mark as Answer" on this post.

Thanks,
Page Brooks
Silverlight MVP, MCSD
PageBrooks.com | @pbrooks
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities