I am working on ASPx Web Forms Page and have problem with printing page.
I have button 'Print' which calls event (print method):
private void MenuPrint_ItemClick(object sender, DevExpress.Web.ASPxMenu.MenuItemEventArgs e)
{
Response.RedirectOn("Print.aspx", "_blank", "menubar=0,scrollbars=1,width=780,height=900,top=10");
}
after I press 'Print' button it opens new window 'Print.aspx' and this is which is on this Page:
protected void Page_Init(object sender, EventArgs e)
{
LoadData(); // generate print document
Response.Write("<script language=javascript>window.print();</script>");
}
and now it's the problem:
window.print(); will open google chrome printing menu, which block old window (that window where is 'Print' button.
When I close Print.aspx by clicking [X] List.aspx windows will be still blocked. When I press 'Anuluj' - which means Cancel and then press [X] List.aspx won't be blocked. Eveything will be fine.
I make some research and figure out that there is no more options to print document, also I can't handle 'Cancel printing' button.
Question is how to avoid that block, maybe I should use something other instead RedirectOn?
This seems to be a bug of Chrome 34 - is this the Chrome version you are using?
There is a similar question open here: Javascript window.print() in chrome, closing new window or tab instead of cancelling print leaves javascript blocked in parent window.
No solution so far though.
Related
I have a C# 4.0 WinForms application, which has a WebBrowser control and 2-buttons.
Clicking the first button sends a URL to the browser to navigate to a specified webSite.
Clicking the second button parses the OuterHtml of the webBrowser1.Document, looking for an "https://..." link for File Download.
The code then uses a webClient.DownloadFileAsync to pull down a file for further use in the application.
The above code successfully works, if I manually click those buttons.
In an effort to automate this for the end-user, I place the first button's click event, i.e. btnDisplayWeb.PerformClick(); in the form's Form1_Load event. This also works, allowing the webBrowser1 to populate its Document with the desired webSite.
However, I am unable to programatically click the 2nd button to acquire the web link for file download.
I have tried to place the 2nd buttons click event within the browser's DocumentCompleted event, as shown below.
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
btnMyUrl.PerformClick();
}
However, from what I've read on StackOverFlow and other sites, it's possible that this particular event gets called more than once, and hence it fails.
I've also attempted to loop for a number of seconds, or even use a Thread.Sleep(xxxx), but the browser window fails to populate until the sleep or timer stops.
I attempted to use the suggestions found on the following StackOverFlow site shown below.
How to use WebBrowser control DocumentCompleted event in C#?
private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
string url = e.Url.ToString();
if (!(url.StartsWith("http://") || url.StartsWith("https://")))
{
// in AJAX
}
if (e.Url.AbsolutePath != this.webBrowser.Url.AbsolutePath)
{
// IFRAME
}
else
{
// REAL DOCUMENT COMPLETE
}
}
However, in parsing the OuterHtml, nothing is returned in the first two sections, and in the third section, other elements are returned instead of the desired "https://..." link for File Download.
Interestingly, if I use a webBrowser1.ReadyState event, as shown below, and place a MessageBox inside DocumentCompleted, this seems to allow the browser document to complete, because after clicking the OK button, the parsing is successful.
if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
{
MessageBox.Show("waiting", "CHECKING");
btnMyUrl.PerformClick();
}
However, I then have the difficulty of finding a way to click the OK button of the MessageBox.
Is there another event that occurs after the DocumentCompleted event.
OR, can someone suggest how to programmatically close the MessageBox?
If this can be done in code, then I can perform the buttonClick() of the 2nd button in that section of code.
After finding that the addition of a MessageBox allows the webBrowser1.Document to complete, and using webBrowser1.ReadyState event within the webBrowser_DocumentCompleted event, all I needed to do, was to find a way to programmatically close the MessageBox.
Further searching on StackOverFlow revealed the following solution on the site below.
Close a MessageBox after several seconds
Implementing the AutoClosingMessageBox, and setting a time interval, closed the MessageBox and allowed my button click, i.e. btnMyUrl.PerformClick(); to successfully parse the OuterHtml and now the code works properly.
Hopefully, if someone else discovers that placing a MessageBox within the webBrowser_DocumentCompleted event allows the document to complete; the aforementioned AutoClosingMessageBox will assist them as well.
Please be assume that I have already login.
I have two pages. PickupList.aspx. There has a button named as "New".
When I click New button page url changed as PickupJobForm.aspx?mode=new
But loading time is not finish and form not appear.
After logout the application. And I copy and paste this url PickupJobForm.aspx?mode=new. And I press enter key from keyboard. Login form appears. After I logIn ready, I can see the create job web form.
What is the differences?
One more info is that our client computer is window 10. And we are using the Trend Micro Apex One.
protected void rbtnNew_OnClick(object sender, EventArgs e)
{
Response.Redirect("~/PickupJobForm.aspx?mode=new", false);
}
I can open the link in my standard browser with this code:
public void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
//cancel the current event
e.Cancel = true;
//this opens the URL in the user's default browser
Process.Start(e.Url.ToString());
}
But the problem is that IE only should be opened when a link on the webbrowser is clicked. When using this code IE also opens when I change the documenttext.
My suggestion would be to take a different approach. At the point in time immediately after the initial page has loaded in the WebBrowser control (Navigated event), you can use the webBrowser1.Document property to retrieve an HtmlDocument instance.
From this you should be able to find your link by using, for example,
http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.getelementbyid(v=vs.110).aspx
Then you can add an event handler to detect when this link is clicked, and in this handler, run your code to start the IE process.
I have a problem where my button click will not work whatsoever. I think all the code is right but why is nothing happening when it's clicked? Is the code right for displaying the message and is the code right for redirecting to a website/webpage? Any help much appreciated!
public delegate void myDelegate();
public event myDelegate FindInfo;
protected void btnOne_Click(object sender, EventArgs e)
{
FindInfo += new myDelegate(showFindInfoMessage);
FindInfo += new myDelegate(showWebsite);
FindInfo();
}
public void showFindInfoMessage()
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "scriptkey", "<script>alert('You will now be redirected to the website!');</script>");
}
public void showWebsite()
{
string web = "https://facebook.com/";
Response.Redirect(web);
}
HTML
<asp:Button ID="btnOne" runat="server" Text="Find Info" OnClick="btnOne_Click" />
Your code looks a little strange, but it should work as you expect. I don't see anything that could immediatelly stop it from working.
Easiest things first - are there any exceptions thrown? Run the site from within VisualStudio in Debug mode, click the button on the site, and check out the "Output" window. If you see any "First Chance Exception" lines showing up after you clicked it, then try to chase and solve them, or write them down and show them to us. Make sure that those were really recorded only after you clicked the button. There might be some exceptions recorded when the site starts up. After starting up the site, be sure that the Output window stops chattering before you click the button.
Next, does any other buttons you create work as expected at all? On this page? How's that on other pages? Have you tried placing a breakpoint to see if this code runs at all? Place a breakpoint on the first line of btnOne_Click method (click there and press F9, assuming you have typical hotkeys), and also try placing some other breakpoints in other methods, like Page's constructor. Then try running your site in "debug" mode and click on the button. Check which breakpoints get triggered.
In case NO breakpoint gets hit at all, then either your VisualStudio and webserver are not configured for debugging (oops), or the page might not be compiled at all (for example, you may be running an old copy of it).
If some breakpoints are triggered but that one button handler is not, then try observing what your page does or tries to do when you click that button. If you are using any modern browser, it should have a "developer tools" panel somewhere. For example, in Chrome, press F12. In those dev-tools, find some tab or window that will show you the "Network operations". Display or refresh your page, check if some netwrok operations are captured. You should notice at least the page itself being downloaded with "200" status code. After observing that, when everything gets silent, "Clear" the log (there should be a button for that somewhere) so you get a fresh&clean view again, and press the button. Observe what happens.
If the button works on client-side, you will see a request being sent to the server. Observe the response code. Is it 200? 404? 500? All of those will mean different things. Report back with the code. Also, if there are any error (status code != 200) try to read/copy all of the details you can.
If the button and the server work ok or almost ok, then after clicking the button you would see a request sent back to the same page, followed by a status=200. However if IIS and ASPX are OK, this would run the xxx_Click handler. So probably you won't see this scenario.
If the button doesn't work at all and no requests are sent, then there's some problem with the HTML code that was rendered. Go back to the dev-tools in the browser, find a tab/window that allows you to see the raw HTML code of the site, and find that button. Check what click actions are set on it. If you don't see any, check which FORM tag is the parent of that button and check if its "method" and "action" attributes are set properly. If the method/action are generated wrong, the browser may try to send the 'click' to a wrong place, or even it may not try sending it at all.
If all of that does not show anything useful, then try ... temporarily replacing your code with something that should work in any case. Check out the example at MSDN site. Backup your page code somewhere and replace it with the example from this site. Paste all of that as the ASPX file, and remove everything from the code behind (ASPX.CS file), and run your site. This example shows a simple button and attaches one handler to that button. Check if it works. It should. Also you may observe all of the things I wrote above again. If the example does not work, then, again, I'd guess your IIS or ASP installation(s) are broken.
Your code works correctly. I put it in a test project and it redirects. If you are asking why you do not see the alert, that is because the page would need to post back to register the script, however you redirect to facebook.com before it has a chance to do so. You can register the script on page load, then call the JavaScript function to display the alert.
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
showFindInfoMessage();
}
public delegate void myDelegate();
public event myDelegate FindInfo;
protected void btnOne_Click(object sender, EventArgs e)
{
FindInfo += new myDelegate(showFindInfoMessage);
FindInfo += new myDelegate(showWebsite);
FindInfo();
}
public void showFindInfoMessage()
{
Page.ClientScript.RegisterOnSubmitStatement(this.GetType(), "scriptkey", "alert('You will now be redirected to the website!')");
}
public void showWebsite()
{
string web = "https://facebook.com/";
Response.Redirect(web);
}
}
I'm hosting YouTube Embedded Player in C# WebBrowser control. When I click button "Watch on YouTube" IE opens. Is there any way to open the link in default web browser, for instance, Chrome?
Like this for example:
private void browser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
System.Diagnostics.Process.Start(e.Url.ToString());
e.Cancel = true;
}
Explanation: change the navigating handler to process the URL and fire the link in the diagnostics (which will open the URL in the user’s default browser).