How can I preview html file in another window? - c#

Hy,
In my application I store a html code in a string, for example:
string myHtml = "<html><body><input type ='text' value='hello'/></body></html>";
How can I preview this html in another window pressing some button?
<asp:Button ID="PreviewButton" runat="server" Text="Preview" OnClick="PreviewButton_Click"/>
I've tried :
protected void PreviewButton_Click(object sender, EventArgs e)
{
myHtml = "<html><body><input type ='text' value='hello'/></body></html>";
Response.Write(myHtml);
Response.End();
}
And it works, the preview it's opened but in the same window.. does anyone know how can I open it in another window?
Thanks in advance.
Jeff

You could store the source in a javascript string and use window.open and then write to the new window the source.
Remeber to escape the source correctly first.

You can call window.open in Javascript to open a new window showing an ASHX handler that serves your HTML. (you may need to pass information on the query string)
Remember to set the Response.ContentType in the handler.

Related

Can I use Response.Redirect without encoding?

I want to achieve that a file that is described by URL or network drive (N:\ESSBP1-Office\Imagenes\Check list.xlsx) can be opened through a browser by using the resonse.redirect function in c#.
BUT: This only works for me if the path doesn't contain a space.
How can I avoid the encoding? If I type that same path into the InternetExplorer the address - with space - is resolved correctly and the file is opened.
I didn't manage to avoid the encoding with the response.redirect function. Server.UrlEncode() didn't do that neither, as it just adds another unwanted encoding, while I wanted the exact content of a TextBox/Label to be the string passed to the browser's URL-bar, followed by hitting the enter key - which should be done through code.
I finally achieved that, following #Richard Housham´s suggestion using Javascript:
Mark-up:
<asp:TextBox id="TB_ExternalLink" runat="server"/>
<asp:LINKButton ID="LB_Open" runat="server" OnClick="OpenLink">Open Link</asp:LINKButton>
<input type="hidden" runat="server" id="hiddenExternalFile"/>
Code:
protected void OpenLink(object sender, EventArgs e)
{
string url = TB_ExternalLink.Text.Replace(#"\\", #"\\");
hiddenExternalFile.Value = url;
Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "windowOpen()", true);
}
Javascript:
function windowOpen() {
Window = window.open(document.getElementById('hiddenExternalFile').value, "_blank");
}
Opens any link that is typed into the textbox in these formats in a new tab:
http://www.url.com
\\corp.root.int\INT-Host$\USR03\XXX\W10\Desktop\Kaizen MRO\RMA_Part.png
N:\ESSBP1-Office\Imagenes\Check list.xlsx
regardless of spaces or special ASCI characters

C# Winform Webbrowser not updating after document text update

Good Morning,
I have a Web browser embedded within a C# winform. When loading the web-browser, it loads in a local file and displays the page with no issues.
I then have a button with an OnClick method which does the following:
private void button1_Click(object sender, EventArgs e) {
this.webBrowser1.Navigate("about:blank");
HtmlDocument doc = this.webBrowser1.Document;
doc.Write(String.Empty);
this.webBrowser1.DocumentText = //PathToDocumentText;
}
This was taken from this SO question and causes the web browser to freeze up. On hover shows the cursor with the loading spinning icon.
I am simply wanting to change the document text from one local file to another (both work if I load them in manually OnLoad).
Any help appreciated.
this.webBrowser1.Navigate("about:blank");
this.webBrowser1.Document.OpenNew(false);
this.webBrowser1.Document.Write(//pathtoFile);
this.webBrowser1.Refresh();
This does the trick, Thanks to anyone who looked at this.

Is it possible to add 2 Response.Redirect, one will open in different tab and other one will open in same tab, using C#?

When user will click on button, I want to open one .aspx/.html page in different tab and open one .aspx/.html page in same tab.
Sample code:
string redirect = "<script>window.open('../User/Profile.html');</script>";
Response.Write(redirect);
Response.Redirect("../User/NewUser.aspx",true);
Thanks in Adance!!!
No, the response redirect writes in the http's header the "location" value and can only have one, but you can write a javascript like the next for do what you need:
window.open('../User/Profile.html', 'tabName');
window.location.href = '../User/NewUser.aspx';
Good luck!
We can achieve by using Javascript and code behind page
Call Javascript Window.Open() function on Clientclick property to open in new window.
and onClick call your code behine buttonClick Event to redirect on same window.
ASPX Page:
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" OnClientClick="javascript:window.open('http://google.com','_blank');return true;" />
Code behind On Click function:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("http://google.com");
}
This code not working on Chrome :
window.location.href = '../User/NewUser.aspx';
But you can use this code instead of window.location.href then its working in all browsers :
setTimeout(function(){document.location.href = "page.html"},500);

Load file and fill a page after click

I have a simple request. I have this:
So, in my .aspx :
<asp:FileUpload ID="myFileUpload" runat="server" onclick="LoadFile_Click" />
After the pression of the "Load file" button, I want that the page will be filled with the data of the document choosen. Like this:
In my .aspx.cs
protected void LoadFile_Click(Object sender, EventArgs e)
{
string address = // a way to get the path of the file. How?
BindExcelToPage(address); // this method fills the web page with the data taken from the file uploaded;
}
The informative phrase "No file selected" (or whatever) must be invisible. I want just the Load button.
I don't know how to implement the starts of the event for the loading of the page.
After you BindExcel file with page, hide your fileupload control and replace with it label(if you want use label and button) and set the filename in label. This way it looks like file is there. FileUpload control's filename is readonly, so you can't assign it.

Gecko usage in C# (geckofx)

There are some things that I didn't find how to do using geckofx:
Get the URL of a clicked link.
Display print preview window.
Does this functionality exist in geckofx? If not, what's the best way
to achieve it in a C# project that uses GeckoWebBrowser to display html pages?
Thanks
To get url of clicked link you can use:
void domClicked(object sender, GeckoDomMouseEventArgs e)
{
if(geckoWebBrowser1.StatusText.StartsWith("http"))
{
MessageBox.Show(geckoWebBrowser1.StatusText);//forward status text string somewhere
}
}
To show print dialog box you can use:
geckoWebBrowser1.Navigate("javascript:print()");
OnNaviagted event should give you the link, and look for the print interfaces nsIPrintingPromptService::ShowPrintDialog in Geckofx.
geckoWebBrowser.url
That will give you the url at any point I believe where geckoWebBrowser is the name of the control, however as pointed out you'll be able to get it from the document completed and navigated events using e.url .
For printing, see this forum thread. Make sure to read it all before starting. Essentially you'll have to patch and recompile GeckoFX.

Categories