14 Ekim 2011 Cuma

HTML Document Extensions Methods

        public static void AddScript(this HtmlDocument htmlDocument, string javaScript)
        {
            HtmlElement head = htmlDocument.GetElementsByTagName("head")[0];
            HtmlElement scriptElement = htmlDocument.CreateElement("script");
            IHTMLScriptElement element = (IHTMLScriptElement)scriptElement.DomElement;
            element.text = javaScript;
            head.AppendChild(scriptElement);
        }

        public static void AddCSS(this HtmlDocument htmlDocument, string cssFileName)
        {
            IHTMLDocument2 currentDocument = (IHTMLDocument2)htmlDocument.DomDocument;
            int length = currentDocument.styleSheets.length;
            IHTMLStyleSheet styleSheet = currentDocument.createStyleSheet(@"", length + 1);
            using (TextReader reader = new StreamReader(cssFileName))
            {
                styleSheet.cssText = reader.ReadToEnd();
            }
        }

        public static void DoPostBack(this HtmlDocument document)
        {
            document.InvokeScript("__doPostBack");
        }

        public static void DoPostBack(this HtmlDocument document, string eventTarget, string eventArgument)
        {
            document.InvokeScript("__doPostBack", new object[] { eventTarget, eventArgument });
        }

        public static HtmlElement GetElementByTitle(this HtmlDocument document, string title)
        {
            return (from x in document.All.Cast<HtmlElement>()
                    where x.GetAttribute("title") == title
                    select x).SingleOrDefault();
        }

Hiç yorum yok:

Yorum Gönder