Currently I am building a windows form app using c#. I have a web browser control on my form that displays a google ad. When clicked the webpage is displayed within the little 300x300 web browser control on the form. Is there a way for me to launch the default browser when the ad is clicked instead?
Thanks.
Edit: I figured out I can do so open the default browser by using Process.Start("url here"); but the browser windows is opening upon app load.
Edit Adding Code:
Form.cs
private void AdPanelNavigating(object sender, WebBrowserNavigatingEventArgs e)
{
e.Cancel = true;
if (e.Url != null ) Process.Start(e.Url.ToString());
}
Form.Designer.cs
this.AdPanel.Navigating += new WebBrowserNavigatingEventHandler(AdPanelNavigating);
You can add Navigating event handler:
webBrowser1.Navigating += new WebBrowserNavigatingEventHandler(WebBrowser_Navigating);
void WebBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e) {
e.Cancel = true;
Process.Start(e.Url);
}
It will open default browser instead of opening this url inside webbrowser.
Related
I am using webbrowser into Windows application.
When webbrowser navigate first time, there is no problem.
But calling second time webbrowser navigate captcha does not change.
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate("uyg.sgk.gov.tr/SgkTescil4a");
webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.AllowNavigation = true;
}
To prevent page caching on your client, which may be the reason why you see the Captcha not changing use
webbrowser1.Refresh(WebBrowserRefreshOption.Completely).
to clear the cache. If its not a client side caching issue, then its a problem with the website.
My webview display a html page which contains lots of links, if I click the link, the new page will open in the webview, is there a way to open the link in default browser rather than in webview?
You can handle the WebView.NavigationStarting event as follows:
private void navigationStartingHandler(object sender, WebViewNavigationStartingEventArgs e)
{
e.Cancel = true;
Process.Start(e.Uri.ToString());
}
Here and here are more answers about opening web pages in default browser
I'm using a WebBrowser Control on a WPF application that opens a website with a form for the user to fill and submit. After submitting, it redirects to another page after 10 secs, www.google.com for example.
Is there a way to detect when the WebBrowser has opened www.google.com?
You can detect when the WebBrowser has opened your site name in webbrowser_LoadCompleted event,try this:
void webbrowser_LoadCompleted(object sender, NavigationEventArgs e)
{
mshtml.HTMLDocument doc =( mshtml.HTMLDocument) s.Document;
if (doc.parentWindow.window.location.host == "www.google.com")
{
MessageBox.Show("you navigated to google");
}
}
As far as I understand, Response.Redirect("http://stackoverflow.com"); tells the browser to initiate a request to another URL.
One of the results of this is that the browser "remembers" the redirection, and allows pressing "back."
However, I have a website where Response.Redirect disables the ability to press the browser's "Back" button, as if the browser had opened a new window. (Browsing history is not forgotten, unlike with Server.Transfer.)
The redirection used to work properly in the past, so I suspect the problem has something to do with the IIS server (IIS7).
I apologize in advance if this question should be moved to ServerFault.com.
UPDATES:
Here is some code:
protected void btnClickMe_Click(object sender, EventArgs e)
{
// ...
// some server-side logic
// ...
Response.Redirect("NewPage.aspx?ProductID=" + idNum);
}
Regarding "disables the ability to press the browser's 'Back' button", what I meant is that the button cannot be pressed. Same as when you open a new window. The button is gray, and clicking it has absolutely no effect.
UPDATE 2:
This has been tested with IE6 and IE8.
The problem was NOT with the Response.Redirect();.
When I was on OldPage.aspx, I entered a new URL in the address bar. Once the browser loaded the new site, it disabled the back-button.
Conclusion: There is something wrong with OldPage.aspx, not the redirection to NewPage.aspx.
I still don't know why THIS happens, but this is an entirely different question.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) //check if the webpage is loaded for the first time.
{
ViewState["PreviousPage"] =
Request.UrlReferrer;//Saves the Previous page url in ViewState
}
}
protected void btnBack_Click(object sender, EventArgs e)
{
if (ViewState["PreviousPage"] != null) //Check if the ViewState
//contains Previous page URL
{
Response.Redirect(ViewState["PreviousPage"].ToString());//Redirect to
//Previous page by retrieving the PreviousPage Url from ViewState.
}
}
Have a unique customer request which Im unsure how to tackle.
The customer has a webpage form with a browse button to select a file. When the browse button is clicked, instead of showing the local files, they want to pop-up a window with a textbox to enter a code. This code is then used to select a file from a local folder containing 1000 files each with their own code. They want to prevent the user from viewing the other files in that folder.
I did write a custom Windows form to mimic the webpage form but they already have the webpage online and would like to reuse it.
Any ideas how to intercept the browse button? I can use a C# Application with the web browser component, but can that intercept the browse button?
The only option that I can see working is using a C# Application with the web browser component. You can then use WebBrowser.ObjectForScripting to provide a method that can be called to trigger your custom picker window through Javscript, e.g:
window.external.ShowPickerWindow();
You then have two options:
Interrogate the DOM of the page once it's loaded and replace the button with one that triggers your picker window.
Have the customer change their page so it checks for the existance of a window.external.ShowPickerWindow method and basically does option (1) for you.
You can then have a method, perhaps called window.external.GetPickedCode() to pull the code out in the page.
Rob kinder steered me along the correct thinking track by saying "replace the button" which has lead me to a solution which works beautifully!
In short, I hide the browse button, insert a new button next to it that when clicked, opens a new window with a textbox. This textbox then sets a string value in the parent form which is used onSubmit to attach the file.
private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlElement btnBrowse = wb.Document.GetElementById("fiPhoto");
if (btnBrowse != null)
{
HtmlElement newbtn = wb.Document.CreateElement("input");
newbtn.SetAttribute("id", "btnLoad");
newbtn.SetAttribute("type", "button");
newbtn.SetAttribute("value", "Load");
newbtn.Click += new HtmlElementEventHandler(newbtn_Click);
btnBrowse.Parent.AppendChild(newbtn);
btnBrowse.Style = "display:none";
}
HtmlElementCollection forms = wb.Document.Forms;
if (forms.Count > 0)
{
HtmlElement form = wb.Document.Forms[0];
form.AttachEventHandler("onsubmit", delegate(object o, EventArgs arg)
{
FormToMultipartPostData postData = new FormToMultipartPostData(wb, form);
postData.AddFile("photo", photo);
postData.Submit();
});
}
}
private void newbtn_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.ShowDialog();
}
FormToMultipartPostData is too big to post in here but it basically manually constructs the Content-Disposition to be posted
Don't show the actual file browser, imitate one which is showing only that one file in in.
Or since you know the file path when correct code is entered copy the file to temp folder you created and open file browser to browse that folder and it will be contain only that file.