I have an ASP.NET button in my application, which I want to open another website on my server in a new window. Currently I do a Response.Redirect in my server side on click event (to "..\OtherWebsite") which works. But as it needs to be in a new window I need to do it in JavaScript.
Would '..\OtherWebsite' path work in the window.open JavaScript command? If not, what .NET methods/properties can I use to get the full path?
I can't hard code the website URL as some users will be accessing through a LAN (server name\application) and some through the website (www.website.com/application)
You can use self.location.href to get the currect url of the page, next form your new url relative to that and then pass it to window.open() method.
Here is the definition of window.open method.
Hope this helps...
You can do this by executing the javascript from the codebehind.
public void OpenNewWindow(string url)
{
ClientScript.RegisterStartupScript(this.GetType(), "newWindow", String.Format("<script>window.open('{0}');</script>", url));
}
http://forums.asp.net/t/1001747.aspx/1
First result from searching for "open new window from codebehind c# .net"
Then just pass the relative url like so.
String url = "~/page.aspx";
url = Page.ResolveClientUrl(url);
OpenNewWindow(url);
also a reference here: Response.Redirect to new window
Hope this helps.
Related
I have a C# WinForms application which I need to open a url through it.
The actual task is to display a web page without all it's functionality (changing url/go back button/etc...), do some actions in that site and then retrieve information from it according to what the user entered/did in that site.
Iv'e already tried the WebBrowser option, but it's opening the url site in a browser with all of it's functionality:
System.Windows.Forms.WebBrowser WebBrowser1 = new System.Windows.Forms.WebBrowser();
WebBrowser1.Navigate(new Uri("https://stackoverflow.com/questions/59766190/open-a-url-site-in-a-winforms-window-and-not-from-the-browser"), true);
WebBrowser1.BringToFront();
Any ideas?
Thanx :)
You're passing true as the second argument to the Navigate(Uri url, bool newWindow) method, which specifies that it should open the url in a new window (see the Microsoft documentation).
Changing your code so that it passes false for the newWindow argument will cause the url to be opened in the WebBrowser control instead (you also need to add the control to the form's Controls collection):
System.Windows.Forms.WebBrowser WebBrowser1 = new System.Windows.Forms.WebBrowser();
this.Controls.Add(WebBrowser1);
WebBrowser1.Navigate(new Uri("https://stackoverflow.com/questions/59766190/open-a-url-site-in-a-winforms-window-and-not-from-the-browser"), false);
I'm trying to give the user a new browser window and redirect them to a different page. I've done it before using
ScriptManager.RegisterStartupScript(Page, typeof(string), "popup","window.open('ClaimTimeExpense.aspx', '_blank')", true);
The thing with this is that the current page that I'm on has a URL/file path of for example,
localhost/protected/ADMIN/AllUsers.aspx
but the page I need to redirect to is just
localhost/protected/myPage2.aspx
so when the window open up it throws server error because it's puttin the file path to..
localhost/protected/ADMIN/ClaimTimeExpense.aspx
Is there something wrong in my script manager line or any way to get the url to point correctly? I would use javascript but it's required to be this way.
Try this one so as to open localhost/protected/myPage2.aspx:
window.open('../ClaimTimeExpense.aspx', '_blank')
Does anyone know how to pass a url parameter to a local page in the Web Browser Control?
When you navigate to the page alone "/Html/MyPage.html" all is well but as soon as you add a parameter "/Html/MyPage.html?Message=Hello" I get an error page stating that we could not navigate to the page.
Any ideas?
As a dirty workaround that just works you can implement this as following:
A. Navigate to the page w/o any parameters
B. Attach arguments passing logic below to one of the following events
WebBrowser.Navigated Event - when successfully navigated
WebBrowser.LoadCompleted Event - occurs after the WebBrowser control has loaded content.
C. Inject arguments to html page using webBrowser.InvokeScript (C#)
webBrowser.InvokeScript("eval", new string[] {"processArgs('someArgs') or any generated/custom script"});
or
webBrowser.InvokeScript("processArgs", new string[] {"someArgs"});
where processArgs is defined somewhere in your html file.
As another workaround you can pass your arguments as location hash parameter (if it is not used)
browser.Navigate(new Uri("www/index.html#p=123&p2=567", UriKind.Relative));
and then in index.html
var args = window.location.hash;
(args = '#p=123&p2=567')
Tested on WP7 (index.html is stored in isolated storage) + WP8 (index.html is loaded directly from XAP)
I need help with connecting to a certain website via my username & password.
With WebClient I can fill the username field and the password field, but how do I invoke the click method of the button?
And How can I fill a specific textBox that doesn't have an ID?
I tried doing this with webBrowser, but every time I navigate I have to use a new function every time, which makes the work much harder.
Thanks.
What you're trying to do is wrong. If you want to Post some data to a web address (a URL), simply create a web form (a simple HTML form), fill it, and then send it. Just consider these notes:
Your HTML's form action should be the exact URL of the form you're imitating.
Your input controls should have the same name attribute value.
For more information, see Form Spoofing
Look at the web browser control and see if you can use that inside your windows form to perform the task that you are doing. Once you are satisfied with the results, you can make the web browser control invisible, and it'll work just like you do with web response and request calls.
View the source code and find the id of the button (say "Login").
Then use:
HtmlElement elem = webBrowser1.Document.GetElementById("Login");
if (elem != null)
elem.InvokeMember("click");
I have a page that calls another page with some query string parameters. I want to return back to that page after clicking on a button.
I have to mention that I write that code in a user control and I don't know what page called that second page.
Is there something like Back button in browsers?
Simplest way use javascript on client side with
window.back();
For server side you need to save the url referer in page_load:
if(!Page.IsPostback)
{
ViewState["GoBackTo"] = Request.UrlReferrer;
}
and on a button click using Response.Redirect:
Response.Redirect( ViewState["GoBackTo"].ToString() );
edit: please note ppumkin's comment below!
You could look at Cross Page Posting.
Alternatively, if you are generating the link programatically you could include the returnUrl in the url e.g. http://localhost/secondpage.aspx?returnurl=firstpage.aspx
You can then read this querystring parameter in the secondpage and perform as redirect back once your work is done.
You can use the Request.UrlReferrer, but it is not necessarily sent from the client all the time:
Response.Redirect(Request.UrlReferrer.AbsoluteUri);
put this line of code on the page load event
Btn_Back.Attributes.Add("onClick", "javascript:history.back(); return false;");