I have this code implemented in my application, but whenever i click onto the link, it do help me open a new window. But the original page was "refresh", it kept go all the way back to the top. How can i resolve this problem?
code:
Response.Write("<script>window.open('" + url + "')</script>");
As I understand it, each time you click the link the page is being send to the server where the event is handled (with some C#). If you do that, the server will send the whole page back.
You probably want to control this on client side, with some Javascript.
That said, what you are problably looking for is the attribute target of the link:
something
That will tell the browser that you want to open another tab or windows when the user click the link, and then there request the page specified by url in that tab or window.
Sounds like you want Response.Redirect(myURL)
When you click on the link and handle it in the code behind, that means the link is run at the server side, so it has to post back, which makes it look like it's "refreshing", but it's actually posting back.
You need to handle the opening of the new window on the client's side via Javascript.
If your're writing this to your page, this will redirect you to the url you want
Response.Write("<script>;location.href='" + url + "'</script>");
Related
I am creating an Email Sending Software. So far i am able to open chrome and fill the details of the email (subject,body,attachments and all) but i wanted to know is there any way i can click Send button of G-Mail programmatically too?
Code for filling details in chrome
Process ps = Process.Start("chrome.exe", "https://mail.google.com/mail/u/0/?view=cm&fs=1&tf=1&to=a#gmail.com&bcc=b#gmail.com);
Note I haven't used SmtpClient to send email because it was not fulfilling my customized requirement (sorry cannot disclose the reason).
The way you want this, is not possible. Process.Start opens a new Process. In this case it opens Google Chrome, with the URL you sent with it. This URL contains a querystring containing the subject and the body of the email. This is possible, because Google reads this on the server-side, and puts it in the right fields. There is no way to enter the send button though. This is a POST action, which can not be triggered by a URL.
C# form, does have a WebBrowser class. From here you can access buttons and click them, but I don't think Google will allow this, and most likely send you a captcha. (That is, if you manage to login in the first place.)
I can't help with the simulated button press you're looking for, but I can suggest that you rather try to use the Gmail API to do what you're trying to do:
https://developers.google.com/gmail/api/?hl=en
This would be a more reliable and stable way to send gmail programmatically, and you still don't have to use the smtp object directly.
You should probably use Selenium (WebDriver). It allows you to control browser from c# code, navigates pages, traversing DOM etc..
I have to make a console application in C# which retrieve some data from webpages.
I have downloaded the HTML code from the main page of a website.
WebClient client = new WebClient();
String htmlCode= client.DownloadString(linkToWebpage);
I have verified the string and it is good.
After this part, I have searched for a specific line in the html code which contains a button and a link.
<a rel="nofollow" class="link" onclick="loadornot()" href="http://aaaaa.com/D?WUrtlC1" target="_blank">Click to read more</a>
Now i am trying to download html code from the ancored link (the one from the href), but I am redirected to the main page and I am not sure why. Even if I copy the link from href and paste it into a webbrowser, I am redirected to the main page.
I believe that this happens because the button call a function onclick="loadornot()". That's why it doesn't work the way I have tried? And if yes, how could I call that function from my c# application to continue my app?
Thank you.
Edit:
I have found out that I need some cookies, more exactly, sessioncode, to make that link work. How can I do that?
You can't run javascript code from web page without browser. So, if you really need to execute that function in downloaded page, use some kind of headless browser, like those: webkitdotnet or awesomium
In bleow code Page is opening in a new window. My requirement is to open it in a new tab.
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "popup",
"window.open('" + strFilePath + "','_blank')", true);
You can't open a new tab because that's up to the browser to decide. The user can configure his browser to open a new window on a separate tab. In the latter case, your code will work.
If you want the user to have a windows opened in a new tab, you can only display a link and have the user, right click it, etc.
Note, that clicking a link can still cause a postback on the current page.
You'll just use a redirection on your postback.
http://msdn.microsoft.com/en-us/library/ms153108.aspx
Never waste your effort controlling client environment, I have done it many times, may be disabling right click or opening in new window, and finally had to live up with client settings for my code or hack to work the way I expected it to.
In the mean time have a look at :-
Open a URL in a new tab (and not a new window) using JavaScript
I'm trying to download file from FTP using javascript, for which I created the following topic:
Is it possible to download file from FTP using Javascript?
From there I learned that I can use window.open('ftp://xyz.org/file.zip'); to download the file. It opens a browser new window, but the window closes immediately.
How I can I force it to stay open?
Actually I do all these in Silverlight application:
Here is the code:
HtmlPage.Window.Eval("window.open('" + url+ "', 'Download', 'height=500,width=800,top=10,left=10');");
I also tried this,
string targetFeatures = "height=500,width=800,top=10,left=10";
HtmlPage.Window.Navigate(new Uri(url), "_blank", targetFeatures);
But both results in same : it opens a window, and closes it immediately. I see it just for fraction of second!
I know this doesn't answer your question, and I'm sure you know all of this. I'm answering more because I don't see this point brought up often. :)
Silverlight has very limited support for client interactions. Javascript is a shim that in my opinion gets overused to try and bypass things that Silverlight was architectured against. It would have been very easy for Microsoft to include FTP support in Silverlight but it was excluded for a reason.
However, Silverlight has great support for webservice interactions. So the recommended way of getting a file would be to call a webservice that would do the FTP transfer for you and then send the contents down to the Silverlight application via the webservice. Possibly even processing it on the webservice side for any business logic etc.
Like I said, I suspect your requirement is to not use a webservice (to pass the bandwith cost onto the user most likely). But it'd be interesting to know more about your business problem instead of your technical problem for the solution you've chosen.
It closes because it triggers file download. You can open two windows - one for message and one to download file, but I thiunk user will know it is downloading...
If I were you, I'd open up a page that has whatever visual/UI stuff you'd want to show the user, and either have a META tag that redirects to the download URL, or has a javascript blurb to fire off said download. That way, your window will stay open, but the download will still start automatically.
to keep it open use
var test = window.open();
test.location = 'ftp://openbsd.org.ar/pub/OpenBSD/2.0/arc/kernels/bsd.ecoff';
and to not open any window use
window.location = 'ftp://openbsd.org.ar/pub/OpenBSD/2.0/arc/kernels/bsd.ecoff';
or make a normal link
Remember that a browser is not meant to "display" (visually anyway) the FTP protocol, and not all browsers will suport it. If you want to allow the user to download something, consider using a normal http:// protocol, and opening a window normally as others have suggested.
If you really need the download to be hosted via FTP, consider your backend ingesting (and caching) the file and return it to the user via http
There is nothing to be parsed on the browser's side, hence it closes. If you want to have the page open, you'll have todo something dirty. Like creating a html (or php) page and serve the content you want the user to see, then with a hidden i-frame which will call the FTP contents.
This way your user will see the content you want them to see, and the file is being downloaded.
I had the exact same problem, Silverlight opening a new window for downloading a file would flash a blank window up briefly and it would disappear again without the file download occurring.
This seemed to happen in IE 8 (not 9 and up) and could be fixed by going into Tools->Internet Options->Security then click Custom level... (for whatever zone your site would be in) and go to Downloads->Automatic prompting for file downloads and make sure this is Enabled (I also have File download enabled below that). This Automatic prompting for file downloads setting seems to be absent from IE 9+.
Another workaround is to not open in a new window, if the target url immediately downloads a file it won't change the current window so there's no difference in UX:
HtmlPage.Window.Navigate(new Uri("\download.ashx?fileid=12345"));
I need to launch a browser, do some work and then make the browser navigate to a URL (in that order).
The first part is of course simple and I have a Process object. I am at a loss as to how to later direct it to the target page?
How do I treat the Process as a browser and make it navigate to the desired page?
Any help, pointers, code snippets appreciated.
Instead of launching the browser & then navigating to the page, just tell the OS that you want to run the URL. Windows will pick the correct browser, and navigate the user to the given URL.
System.Diagnostics.Process.Start("http://www.StackOverflow.com");
If you don't need to do this in production, you could use a testing library such as WatiN to do this:
using WatiN.Core;
//Placeholder page to launch initial browser
IE ie = new IE("http://www.google.com");
DoSomeWork();
//Now navigate to the page you want
ie.GoTo("http://stackoverflow.com");
My first instinct for this question was DDE, but it appears that has been decommissioned in Windows Vista so that is no good. Shame, as it was the only consistent mechanism in Windows for Interprocess Communication (IPC)...oh how I miss Arexx on the Amiga.
Anyhow, I believe the following will work but unfortunately, due to the way it works, it launches Internet Explorer irrespective of the configured browser.
If your application has a Form, then create a WebBrowser control on it. Set this to non-visible as we are only making use of its as a launching device rather than to display the web page.
In code, at the point where you want to show a web page, use the following code:
webBrowser1.DocumentText = "window.open('How to launch a browser and later direct it to a page?', 'BananasAreOhSoYummy');";
What this does is to tell the WebBrowser control, which is just the IE in disguise, to open a new window called 'BananasAreOhSoYummy'. Because we have given the window a name, we can use that line repeatedly, with different URLs, to change the page in that particular browser window. (A new window will be opened if the user has happened to close it.)
I will have a think about an approach that honours the user's default browser choice.
If you don't need the actual instance of IE, you can use the System.Windows.Forms.WebBrowser control.
I think instead of sending the browser a url you could send it javascript that would run and direct the browser to a site.
Not sure if this would work but I see no reason why it wouldn't