Skip to content Skip to sidebar Skip to footer

How To Access Span With Needed Innerhtml?

Say my WebBrowser1 downloaded a page that has a following line: hamolulu It is inside of a td tag, inside of big table, inside of i

Solution 1:

another solution using no js (because you don't own the "page") since it is inside an iframe then you should search within that iframe

HtmlElementCollectioniframes= WebBrowser1.Document.GetElementsByTagName("iframe");
HtmlElementiframe= iframes(0/* iframe index */); // HtmlElementCollectionspans= iframe.Document.GetElementsByTagName("span");
for (i = 0; i < spans.Count; i++) {
    HtmlElementspan= spans(i);
    if (span.GetAttribute("customAttr") == "customAttrValue") {
        stringonclick= span.Children(0).GetAttribute("onclick"); //span.Children(0) should return the <a>
        WebBrowser1.Document.InvokeScript(onclick);
    }
}

Solution 2:

Unless I am missing something...

<span id="hamolulu">hamolulu</span>

Then when you want to change it...

document.getElementById('hamolulu').innerHTML="<h1>Test!</h1>";

Solution 3:

If you set up your span as an HTML server control:

<span runat="server"id="myspan" customattribute="customvalue">hello world</span>

Then you can register an event handler on page load:

protectedvoidPage_Load(object sender, EventArgs e)
    {
        myspan.Attributes["onclick"] = "this.innerText='hamolulu'";
    }

Another way to do it is using Page Methods which would call a page C# method using AJAX.

Solution 4:

you can make it simpler by using JavaScript and invoking it from c# whenever you need to.

WebBrowser1 .Document .InvokeScript ("functionName")

javascript:

functionfunctionName(){

  var spans = document.getElementsByTagName('SPAN');
  for (i = 0; i < spans.length; i++)
  {
    var span = spans[i];
    if (span.getAttribute("customattr") == "hamolulu")
    {
        eval(span.getAttribute('onclick')); // .. be careful "span" has no "click()" method. you should use the onlick attribute if availablebreak;
    }//end if
  }// end for
}

Post a Comment for "How To Access Span With Needed Innerhtml?"