20 Ocak 2016 Çarşamba

c# webbrowser kullanımı

Built-in Visual HTML Editor

How to enable it:
webBrowser1.Document.DomDocument.GetType().GetProperty("designMode").SetValue
    (webBrowser1.Document.DomDocument, "On", null);
How to paste HTML element programmatically:
// appends:
// <a id="mylink" href="http://codeproject.com/">
//   <img id="myimg" 
//    src="http://dj9okeyxktdvd.cloudfront.net/App_Themes/CodeProject/Img/logo250x135.gif" />
// </a>

var mylink = webBrowser1.Document.CreateElement("a");
mylink.Id = "mylink";
mylink.SetAttribute("href", "http://codeproject.com/");
             
var myimg = webBrowser1.Document.CreateElement("img");
myimg.Id = "myimg";
myimg.SetAttribute("src", 
"http://dj9okeyxktdvd.cloudfront.net/App_Themes/CodeProject/Img/logo250x135.gif");
mylink.AppendChild(myimg);
webBrowser1.Document.Body.AppendChild(mylink); 

DOM element selection - GetElementById

var id = "mylink";

var el = webBrowser1.Document.GetElementById(id);
if (el != null)
{
    MessageBox.Show("Element with id=\"" + id + "\" has innerHTML: " + el.InnerHtml);
}
else
{
    MessageBox.Show("Element with id=\"" + id + "\" not found.");
}

DOM element selection - GetElementsByTagName

var tag = "img";

var elz = webBrowser1.Document.GetElementsByTagName(tag);
if (elz.Count > 0)
{
    MessageBox.Show(elz.Count + " elements with tag <" + tag + "> found.");
}
else
{
    MessageBox.Show("No elements with tag <" + tag + "> found.");
}

DOM content manipulate - set HTML

webBrowser1.DocumentText = "<html><head><script>alert('check!');
</script></head><body>lorem</body></html>";

Inject + exec Js

var head = webBrowser1.Document.GetElementsByTagName("head")[0];

var scriptEl = webBrowser1.Document.CreateElement("script");
scriptEl.SetAttribute("text", "function sayHello() { alert('hello') }");
head.AppendChild(scriptEl);

webBrowser1.Document.InvokeScript("sayHello");