Best control for rendering HTML - c#

I have the need to render some html text (not an html page with <html> and <body> tags and everything, just some <i>'s and <hr />s and stuff) in a C# .NET 4.0 Winforms application. Preferably it would be a Panel-like control that you could just p.HTML = "somehtml" on and it would display HTML. Has anyone had any experience with .NET HTML rendering controls that they could recommend? I found this on codeproject but I'm a little wary of stuff on there.

Why not use the build in WebBrowser control. You can always enclose your html snippet in standard <html/> markup.
string html = "<i> some text </i>";
webbrowser1.DocumentText = string.Format("<html>{0}</html>", html);

You can try this one link
You can use the WebBrowser control in
design mode with a second WebBrowser
control set in view mode.
In order to put the WebBrowser control
in design mode, you can use the
following code.
This code is a super stripped down
version of a WYSIWYG editor for one of
our software products.
Simply create a new form, drop a
WebBrowser control on it, and put this
in the form_load
Me.WebBrowser1.Navigate("about:blank")
Application.DoEvents()
Me.WebBrowser1.Document.OpenNew(False).Write("<html><body><div id=""editable"">Edit this text</div></body></html>")
'turns off document body editing
For Each el As HtmlElement In Me.WebBrowser1.Document.All
el.SetAttribute("unselectable", "on")
el.SetAttribute("contenteditable", "false")
Next
'turns on editable div editing
With Me.WebBrowser1.Document.Body.All("editable")
.SetAttribute("width", Me.Width & "px")
.SetAttribute("height", "100%")
.SetAttribute("contenteditable", "true")
End With
'turns on edit mode
Me.WebBrowser1.ActiveXInstance.Document.DesignMode = "On"
'stops right click->Browse View
Me.WebBrowser1.IsWebBrowserContextMenuEnabled = False

Related

Windows phone webbrowser Control is Blank?

Im trying to open a URL(Some link) in Webbrowser Control.
The link return a html page which contain Google Graph , but my Webbrowser Control is Blank and dont display any thing on it. It works fine on WebBrowserTask and on my pc so their is no problem in this link but it is blank on webBrowser Control Any Idea How i can Do this ??
public GraphPage()
{
InitializeComponent();
webBrowser1.Navigated += new EventHandler<System.Windows.Navigation.NavigationEventArgs>(Browser_Navigated);
webBrowser1.Navigating += new EventHandler<NavigatingEventArgs>(Browser_Navigating);
loadPage(getBaseUrl(graphType));
}
private void loadPage(String url )
{
webBrowser1.IsScriptEnabled = true;
webBrowser1.Source = new Uri("Link");
}
As mentioned by user112553, set IsScriptEnabled true. Can be done under the XAML-code or in the code-behind with
XAML
<phone:WebBrowser x:Name="Browser" IsScriptEnabled="True" />
Code-Behind
Browser.IsScriptEnabled = true;
I encountered a similar situation, with Windows Phone 8 and a HTML page using JQuery.
IsScriptEnabled=true wasn't enough (the page didn't render properly).
I solved adding to the html page a
doctype declaration:
<!DOCTYPE html>
<html>
...
Seems like the WebBrowser component refuses to render HTML5 pages without explicit defining the document-type.
Since it's a common problem with rendering pages in IE<11 when not defining this tag, the cause of why my scripts didn't run could be many and most likely a reference to a HTML5 tag which was not handled correctly.
ref: http://msdn.microsoft.com/en-us/hh779632.aspx
Since windows phone 8.0 is based on Internet Explorer 10, it makes sense, the confusing part with debugging this behavior is that Internet Explorer on your phone renders the page perfectly. Still the WebBrowser component will not.
If this is documented in the API specifications, it should be easier to find, because I was not able to find any information that would point me to this solution at all, this would be mostly because my pages was rendered in WebViews for Android and IOS without any problems.
Thanks to Antonio Pelleriti for providing this solution.

webkit-sharp scroll to an HTML anchor tag

I have a webkit-sharp WebView which I am using to display HTMLvia the
LoadString method.
The webview is placed in a ScrolledWindow the ScrolledWindow is placed
in a Gtk Window.
I want to be able to tell the WebView to scroll to a specific part
of the HTML. Normally one would do this using an anchor.
I have defined an anchor and some JavaScript to jump to that anchor, I
call the JavaScript via the ExecuteScript method. This does nothing at
all.
I have also tried adding a button to the HTML that calls the
JavaScript. This also does nothing.
Is there something I can do to make this work, to make it so I can
scroll to a known location in the page?
Update: I can make this work by saving the HTML to a file and then loading from there using a URL which tells it to scroll. However I would like to avoid doing that because of the performance hit of writing the page to disk before displaying it.
How are you adding to ScrolledWindow? I am not GTK expert, but have discovered that it works if you call "add" method and does not work if you call add_with_viewport
Also you may not need to execute the javascript. It looks like you control the html page and can easily access the dom element and call click method on the element.Following sample python code looks for anchor with id "one" and scrolls to desired location without calling javascript.
from gi.repository import WebKit
from gi.repository import Gtk
from gi.repository import GLib, GObject
class WebkitApp:
def exit(self, arg, a1):
Gtk.main_quit()
def onLoad(self, view, frame):
doc = view .get_dom_document()
a = doc.get_element_by_id("one")
a.click()
def __init__(self):
win = Gtk.Window()
self.view = WebKit.WebView()
# Signal Connections
self.view.connect("onload-event", self.onLoad)
file = open("/tmp/epl-v10.html")
html = file.read()
self.view.load_string(html, "text/html", "UTF-8", "file:///tmp")
sw = Gtk.ScrolledWindow()
# sw.add_with_viewport(self.view)
sw.add(self.view);
win.add(sw)
win.maximize()
win.connect("delete-event", self.exit)
win.show_all()
app = WebkitApp()
Gtk.main()
The JavaScript to jump to that anchor, doesn't work while the page is still loading, By waiting for the page load to finish (there is an event for it) then running the script I made it work.
Given an element:
<a id='myElement'>I want to scroll to this line</a>
This can be scrolled to with this JavaScript:
document.getElementById('myElement').scrollIntoView();
Which can be executed in a WebView with the ExecuteScript method, e.g.:
_webView.ExecuteScript("document.getElementById('myElement').scrollIntoView();");

TextArea in WebBrowser

I have a WebBrowser object that I use to navigate to a forum site. Now, I am filling a form that has a textarea I need to interact with.
When I finished writing the topic and submit the value of the textarea, the value didn't set to what I wanted.
WebBrowser browser = new WebBrowser();
browser.Navigate("www.example.com");
browser.Document.All["textarea"].InnerText = "MyText";
browser.Document.All["SubmitButton"].InvokeMember("Click");
Now that didn't work, and one more important thing.. When I use the HTML visualizer to see the page while debugging, The area of the textarea in the visualizer is not displayed it says "This program cannot display the webpage" but the rest of the page is displayed normally.
I heard something about textarea that it is an HTML5 element and because the WebBrowser is an activeX that use the Internet Explorer so maybe I have a version of Internet Explorer that not support textarea elements? I have IE 10.
Can someone please tell how to set the value of a textarea properly.

How can i print a page in asp.net without master page and to change color schemes of the page?

i have made a page in asp.net, i have a costing calculator which has more than 50 fields, dependent on each other, one is the result of previous two and like that, i want my page to be printed in a well manner, and the header of the page which is in master page should not be in print, also the color schemes i want to adjust, let me know the best solution for this which .net provides
Put the content inside <div id="divid">YOUR CONTENT NEEDS TO BE PRINTED</div>
Then call the javascript function on button click which will print the selected area or only html of div. pass the id of div on calling javascript function.
function CallPrint(var strid)
{
var prtContent = document.getElementById(strid);
var WinPrint = window.open('','','letf=10,top=10,width="450",height="250",toolbar=1,scrollbars=1,status=0');
WinPrint.document.write("<html><head><LINK rel=\"stylesheet\" type\"text/css\" href=\"css/print.css\" media=\"print\"><LINK rel=\"stylesheet\" type\"text/css\" href=\"css/print.css\" media=\"screen\"></head><body>");
WinPrint.document.write(prtContent.innerHTML);
WinPrint.document.write("</body></html>");
WinPrint.document.close();
WinPrint.focus();
WinPrint.print();
WinPrint.close();
return false;
}
Call CallPrint('DivGrid');" on onclick() of button or use below: but.Attributes.Add("OnClick", "return CallPrint('DivGrid');");
Nothing to do with .Net and everything to do with a print stylesheet. You can create a stylesheet which will only work for when the page is printed. And you can change everything from what displays to postion to colours.
Use:
<LINK rel="stylesheet" type"text/css" href="print.css" media="print">
Note media="print" means it'll be used for printed pages.
I know that question has been asked a long time ago, however there is no accepted suggestion. So here my approach for friendly print version when using Master page.
Create an empty master page (PrintVersion.Master) to serv as print version. Add what ever you need to be print (like logos) if anything to that master page.
from your content page, add a print link with target blank. Make the href to load the current page. From the href add a querystring so you can capture it from your content page preinit event.
From your content page preinit event detect if the querystring to print exists, then specify the blank master page like: MasterPageFile = "~/Ful/Path/of/your/PrintVersion.Master"
Optional, on the PrintVersion.Master on document.ready call: window.print(); The browser print dialog will automatically open.
You can make a new printable page version, which doesn't include a header. This version can also have the layout you need.

how to print the content of a label or panel in asp.net?

I have a label control in a page in asp.net 2.0 and a button Print.
Clicking on print button I need to print the content of the label and I need same for a panel also.
Is it possible to implement this?
If yes then how to implement that?
Please help.
What about Window.Print() method? Because execcommand method will not work other browsers that IE. Use CSS media option to control the print area.
You would need to add some client side javascript to your Print button to execute the brower's print command. The javascript below could be used to print the whole document page and would be a good place to start. Note: It isn't possible to do this without displaying the print dialog unless you use a third party component.
// Print Page
window.print();
If you wanted just to print certain sections of your page you can achieve this two ways. Firstly, you could render the content to be printed into a hidden iframe and then print just that frame. You would do this using the same code as above only from within the frame itself.
Secondly, you could use a media style print style sheet, a CSS that applies only when printing. Inside this sheet you would set the styles you wanted to print as normal and the styles you didn't want to print to "display:none". The link below contains more information on print stylesheets.
http://www.webcredible.co.uk/user-friendly-resources/css/print-stylesheet.shtml
One more approach could be to open new window and populate the contents of div you wanted to print and have print link/button on that page.
Ex:
var win = window.open(...)
win.document.body.appendChild(document.getElementById('divToPrintId'))
This is how the code will look like this approach is used to print the content/part of the page.
Use below code in button onclick event
ClientScript.RegisterStartupScript(this.GetType(), "PrintOperation", "PrintGridData()", true);
Above link will call function named PrintGridData() which is written in head section as below
<script type="text/javascript">
function PrintGridData()
{
var prtGrid = document.getElementById('<%=GridView.ClientID %>');
prtGrid.border = 0;
var prtwin = window.open('', 'PrintGridViewData', 'left=100,top=100,width=1000,height=1000,tollbar=0,scrollbars=1, status=0,resizable=1');
prtwin.document.write(prtGrid.outerHTML);
prtwin.document.close();
prtwin.focus();
prtwin.print();
prtwin.close();
}
</script>
in this script it will only print div name GridView and other part will not be printed

Categories

Resources