c# - extract text content from web page using asp.net web form -
i'm trying load page may asp.net web form , extract text , display extracted text in areatext
like this:
and code is:
<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" %> <!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <style type="text/css"> #form1 { height: 500px; width: 1199px; } .auto-style1 {} #textarea1 { height: 288px; width: 1157px; } </style> </head> <body> <form id="form1" runat="server"> <asp:button id="button1" runat="server" text="clike me" onclick="button1_click" onclientclick="aspnetform.target ='_blank';" width="160px" cssclass="auto-style1" height="32px" /> <br /> <br /> <asp:radiobuttonlist id="radiobuttonlist1" runat="server"> <asp:listitem>cnn</asp:listitem> <asp:listitem>bbc</asp:listitem> <asp:listitem>fox</asp:listitem> </asp:radiobuttonlist> <br /> <br /> <textarea id="textarea1" name="s1" runat="server" ></textarea></form> </body> </html>
and
using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.io; using system.drawing; using system.threading; using system.windows.forms; public partial class _default : system.web.ui.page { uri url = null; webbrowser wb = new webbrowser(); protected void button1_click(object sender, eventargs e) { wb.documentcompleted += new webbrowserdocumentcompletedeventhandler(displaytext); if (radiobuttonlist1.text == "cnn") { url = new uri("http://www.edition.cnn.com/"); wb.url = url; //response.redirect(url); } else if (radiobuttonlist1.text == "bbc") { url = new uri("http://www.bbc.com/"); wb.url = url; } else { url = new uri("http://www.foxnews.com/"); wb.url = url; } } private void displaytext(object sender, webbrowserdocumentcompletedeventargs e) { webbrowser wb = (webbrowser)sender; wb.document.execcommand("selectall", false, null); wb.document.execcommand("copy", false, null); textarea1.value = clipboard.gettext(); } protected void page_load(object sender, eventargs e) { } }
but have error in line
webbrowser wb = new webbrowser();
activex control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot instantiated because current thread not in single-threaded apartment.
so i'm doing wrong pleas , many in advance
i have never attempted use webbrowser in object-reference, know being web-form means receiving post backs, , if re-instantiate browser reference each time, isn't going operate page object. use page object, can collect controls , methods needed, while utilizing request/response namespaces. match on radiobuttonlist control code below:
protected void page_load(object sender, eventargs e) { if (page.ispostback) { string url; radiobuttonlist rdl = new radiobuttonlist(); url = rdl.selecteditem.text; } }
of course you'd grab .selecteditem.text
markup-based radiobuttonlist, instead of building one.
i checked, , seems webbrowser
object under system.windows.forms
. experience, never want use library in web forms (bad experiences msgbox
).
i'd refactor using sample above ,
response.redirect(url);
hope helps!
Comments
Post a Comment