views:

803

answers:

1

We have a web browser in our Winforms app to nicely display history of a selected item rendered by xslt.

The xslt is writing out <a> tags in the outputted html to allow the webBrowser control to navigate to the selected history entry.

As we are not 'navigating' to the html in the strict web sense, rather setting the html by the DocumentText, I can't 'navigate' to desired anchors with a #AnchorName, as the webBrowser's Url is null (edit: actually on completion it is about:blank).

How can I dynamically navigate to Anchor tags in the html of the Web Browser control in this case?

EDIT:

Thanks sdolphion for the tip, this is the eventual code I used

void _history_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        _completed = true;
        if (!string.IsNullOrEmpty(_requestedAnchor))
        {
            JumpToRequestedAnchor();
            return;
        }
    }

    private void JumpToRequestedAnchor()
    {
        HtmlElementCollection elements = _history.Document.GetElementsByTagName("A");
        foreach (HtmlElement element in elements)
        {
            if (element.GetAttribute("Name") == _requestedAnchor)
            {
                element.ScrollIntoView(true);
                return;
            }
        }
    }
+3  A: 

I am sure someone has a better way of doing this but here is what I used to accomplish this task.

HtmlElementCollection elements = this.webBrowser.Document.Body.All;
foreach(HtmlElement element in elements){
   string nameAttribute = element.GetAttribute("Name");
   if(!string.IsNullOrEmpty(nameAttribute) && nameAttribute == section){
      element.ScrollIntoView(true);
      break;
   }
}
sdolphin
Just found a reference to this online and am testing it now. Will mark as answer when I confirm it.
johnc
Though I am using Document.GetElementByName rather than the loop
johnc
Thank you for posting back your update I changed mine to more closely match yours.
sdolphin