Creating new html elements

10 posts, 1 answers
  1. Telemetry
    Telemetry avatar
    4 posts
    Member since:
    Jul 2010

    Posted 27 Jul 2010 Link to this post

    Hi, I need to be able to create new html elements in the radhtml placeholder. I can do this with C# by using HtmlPage.Document.CreateElement("div"). Is there a way to do this in the html placeholder control? Including OOB? Thanks!
  2. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 30 Jul 2010 Link to this post

    Hi James,

    The HtmlPlaceholder displays html pages / content in an IFrame. You can access this IFrame's content like so:

    HtmlElement iframe = (HtmlElement)myHtmlPlaceHolder.HtmlPresenter.Children[0];
    HtmlWindow contentWindow = iframe.GetProperty("contentWindow") as HtmlWindow;
    HtmlDocument document = contentWindow.GetProperty("document") as HtmlDocument;
    Then you can either look for a specific element on the page, like so:
    HtmlElement content = document.GetElementById("content"); or you can add new element in the HtmlDocument document.

    I hope this information helps. Let us know if we can further assist you.

    All the best,
    Tina Stancheva
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft banner
  4. Telemetry
    Telemetry avatar
    4 posts
    Member since:
    Jul 2010

    Posted 30 Jul 2010 Link to this post

    Thanks so much for the response. This looks like its just what I need. The only problem I'm having so far is that iframe.GetProperty("contentWindow") is returning null. Is that the correct property name?
  5. Telemetry
    Telemetry avatar
    4 posts
    Member since:
    Jul 2010

    Posted 30 Jul 2010 Link to this post

    For whatever reason it looks like whats being passed back from :

    this.iframe = (HtmlElement)this.HtmlPresenter.Children[0];

    is 'div' instead of an iframe.
  6. Answer
    Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 30 Jul 2010 Link to this post

    Hi James,

    Yes this is the correct property. However, if you are trying to access it before the content of the RadHtmlPlaceHolder is loaded, it will return null indeed. Therefore I'd suggest you to move your logic into the RadHtmlPlaceHolder UrlLoaded() event handler.

    I also attached a sample project illustrating a possible approach towards your scenario. Take a look at it and let me know if it helps.

    Kind regards,
    Tina Stancheva
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  7. Telemetry
    Telemetry avatar
    4 posts
    Member since:
    Jul 2010

    Posted 30 Jul 2010 Link to this post

    Thanks Tina this worked perfectly. I didn't notice the UrlLoaded event. Another problem I ran into is that I have some silverlight functions that are exposed to javascript. I've been calling them by using 'document.getElementById('SilverlightControl').Content' and then calling the function. If this is running out of browser, is there a way to call the Silverlight app from the htmlplaceholders javascript? Thanks!
  8. Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1512 posts

    Posted 04 Aug 2010 Link to this post

    Hello James,

    Unfortunately this scenario is not possible. We did some extensive testing but we were unable to workaround it. The reason is as you might expect the OutOfBrowser functionality. However, if you manage to come up with a solution, we'd be more than glad if you share it with us.

    Best wishes,
    Kiril Stanoev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  9. Shaikh Ahmad
    Shaikh Ahmad avatar
    20 posts
    Member since:
    Oct 2012

    Posted 26 Aug 2010 Link to this post

    Hi James,

    I've came across this type of problem and there is a few things that i've tried. I'm not really sure is this what you are looking for but this is how i interact silverlight htmlplaceholder.
    Hope this helps.

    public class HtmlEditor : Telerik.Windows.Controls.RadHtmlPlaceholder
        {
            public HtmlEditor()
            {
                this.Loaded += new RoutedEventHandler(HtmlEditor_Loaded);
            }
      
            void HtmlEditor_Loaded(object sender, RoutedEventArgs e)
            {
                string url = "http://localhost:33333/Editor.aspx";
                this.SourceUrl = new Uri(url);
            }
      
            public string HtmlContent
            {
                get
                {
                    string guid = Guid.NewGuid().ToString().Replace("-", "");
                    HtmlElement iframe = (HtmlElement)this.HtmlPresenter.Children[0];
                    // Set an ID to the IFrame so that can be used later when calling the javascript   
                    iframe.SetAttribute("id", guid);
                    string code = "document.getElementById('" + guid + "').contentWindow.GetHtml();";
                    string html = (string)HtmlPage.Window.Eval(code);
                    return html;
                }
                set
                {
                    string guid = Guid.NewGuid().ToString().Replace("-", "");
                    HtmlElement iframe = (HtmlElement)this.HtmlPresenter.Children[0];
                    // Set an ID to the IFrame so that can be used later when calling the javascript   
                    iframe.SetAttribute("id", guid);
                    string code = "document.getElementById('" + guid + "').contentWindow.SetHtml('" + value + "');";
                    HtmlPage.Window.Eval(code);
                }
            }
        }
  10. Ivan
    Ivan avatar
    8 posts
    Member since:
    Jul 2010

    Posted 27 Dec 2010 Link to this post

    In my case the Url to open in the RadHtmlPlaceHolder is a Silverlight application, so when in the UrlLoaded event I did:

    HtmlElement iframe = (HtmlElement)myHtmlPlaceHolder.HtmlPresenter.Children[0];
    HtmlWindow contentWindow = iframe.GetProperty("contentWindow") as HtmlWindow;
    HtmlDocument document = contentWindow.GetProperty("document") as HtmlDocument;

    But document is always null, I suposse because the silverlight has not loaded yet. Is there any way to wait the silverlight inside the RadHtmlPlaceHolder is already loaded before to get the HtmlDocument?
  11. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 30 Dec 2010 Link to this post

    Hi James,

    When you say that "the Url to open in the RadHtmlPlaceHolder is a Silverlight application" do you refer to a local Silverlight project or an external Silverlight page? Because if the SourceUrl is an external page, you cannot read any of the src document's properties due to security restrictions. If however, you set the SourceUrl to a local page, you should be able to get the HtmlDocument property when the UrlLoaded() event is fired even if the Silverlight page is still loading its content.

    All the best,
    Tina Stancheva
    the Telerik team
    Browse the videos here>> to help you get started with RadControls for Silverlight
Back to Top
DevCraft banner