get URL from web page button click - c#

I have a C# web page viewer working. What I need to do is when a button is clicked within that webpage, it changes the URL that begins with a certain string, say "xyz". I need to know how I could detect this change.
In android I simply used shouldOverrideURlLoading and had an if statement but the only URL I can retrieve is the original one I pass to start the web view.
is there a way to call DocumnetedCompleted after each new screen..There are appox. 2 button presses that get me to the screen with the important button

If you are using a WebBrowser control, you can use the Navigating event to handle when the site navigates to another url.
From there you can use the WebBrowserNavigatingEventArgs to retrieve the new URL and to stop it if you want or change the destination URL.
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
if (e.Url.AbsoluteUri.Contains("something")) {
//stais in the current page
e.Cancel = true;
//aditionally you can navigate to another URL (though it will fire this event again)
webBrowser1.Navigate(e.Url.AbsoluteUri.Replace("something", "empty"));
} else {
//continue
}
}

I think you need something like this
private void change_Url () {
var URL = Request.Url.ToString(); // get the URL
// if you meant to appened the text, then
URL = URL + "abc";
}

Related

How to show an HTML string in WebBrowser control when the address is "about:blank"

I would like to write a specific string (e.g. a help instuction) in the webbrowser control when it navigates to "about:blank", I can write my string in the Form_Load using DocumentText and it automatically navigates to "about:blank"
webBrowser1.DocumentText = "introduction....";
But now if the user refreshes the webbrowser control it shows a blank page. I would like it again shows my string whenever the address is "about:blank". Where is the best place to put my string into the webbrowser control?
A document Refresh simply reloads the current page, so the Navigating, Navigated, and DocumentCompleted events do not occur when you call the Refresh method.
Using Navigating or Navigated event you should check if the browser is navigating or navigated to about:blank then disable the ways that user can refresh page, including browser shortcuts, browser context menu or any other point like custom toolbar buttons and context menus you created or refresh.
For other urls, enable them again.
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
var state = (e.Url.ToString().ToLower() == "about:blank");
this.webBrowser1.WebBrowserShortcutsEnabled = !state;
this.webBrowser1.IsWebBrowserContextMenuEnabled = !state;
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var content = "Custom Content";
if (e.Url.ToString().ToLower() == "about:blank" &&
this.webBrowser1.DocumentText != content)
{
this.webBrowser1.DocumentText = content;
}
}

How to override back button for a page outside of it?

I know how to override back button inside a page:
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
//Do your work here
base.OnBackKeyPress(e);
}
But the problem: I use Inneractive ad service. when I call InneractiveAd.DisplayAd() it shows a new page of its own which doesn't support back button. when it navigate to this page its uri is like this:
/Inneractive.Ad;component/InneractiveFullScreenPage.xaml
The question: is it possible to override back button of that page to navigate back when user presses back?
In HandleBackKeyPress you usually intercept BackKeyPress event, so you would set e.Cancel = true to prevent standard BackKey navigation. To simulate standard navigation you would do something like:
void HookUpBackKeyPress(PhoneApplicationPage page)
{
page.BackKeyPress += HandleBackKeyPress;
}
void HandleBackKeyPress(object sender, CancelEventArgs e)
{
page.NavigationService.GoBack();
}
The problem is to get the reference to the navigated page. The following code will work only after you completely Navigated to the new Page and it might not work when immediately after NavigationService.Navigate(..)
var frame = (PhoneApplicationFrame)Application.Current.RootVisual;
page = (PhoneApplicationPage)frame.Content;

Not able to set Url property at runtime in webbrowser control

I have a webbrowser control in my Winform application.
Below regions belong to division of code sample.
Region 1
The current url page loaded is "http://MyWebsite.com". I am clicking a link (say "About Us") in the web page using code. This click will take me to new url page ("http://MyWebsite.com/About_Us"). In Navigating event I am recording this new url.
Region 2
Now I want to get all elements of this new url and click on a new link. But not sure how to do it. In Region 2 I am also assigning the new url to webbrowser object. But nothing reflects in the instance. webbrowser.url still contains the previous url path.
I have following code for button click:
private void Button1Click(object sender, EventArgs e)
{
// Region 1---------------------------------------------
HtmlElementCollection links = webBrowser1.Document.GetElementsByTagName("A");
foreach (HtmlElement link in links)
{
if (link.InnerText != null && link.InnerText.Equals("Click to view magic"))
{
link.InvokeMember("Click");
break;
}
}
// EndRegion---------------------------------------------
// Region 2---------------------------------------------
webBrowser1.Url = new Uri(_url.AbsoluteUri, UriKind.Absolute);
webBrowser1.Navigate(_url); //New Edit
links = webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement link in links)
{
if ((link.GetAttribute("Name") == "BooHoo"))
{
link.InvokeMember("Click");
break;
}
}
// EndRegion---------------------------------------------
}
private void WebBrowser1Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
_url = e.Url;
}
Can anyone help me to do this. The question may not be very clear. Please let me know if you need any further details. Thanks.
So it was tricky a bit or else I am careless. I was watching the property values in debug mode. Later I noticed that, after pressing F5 in Visual Studio (continue debugging) and all the methods are run, webbrowser shows the changed values.
Hope it helps.
You need to subscribe Navigated event as WebBrowser works asynchronously.
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
--Do Something Here....
}

How to get the URL of a WebBrowser control

if (webBrowser1.Url.AbsoluteUri == "www.google.com")
{
label9.Text = webBrowser1.Url.AbsoluteUri;
}
This is my current code. When I press the button to run this I get the error.
Object reference not set to an instance of an object.
And I don't know why it does that or how to fix it. Any help will be great.
Also It have to work in a timer so that it can be checked.
The Url Property will remain null untill the control is rendered so use this:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
if (webBrowser1.Url.ToString() == "www.google.com") {
label9.Text = webBrowser1.Url.ToString();
}
}
And in your button Click event add:
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
probably your webBrowser1.Url is null
try below to get url
string url = "";
if (webBrowser1.Url != null)
{
url = webBrowser1.Url.AbsoluteUri;
}
if (url == "www.google.com")
{
label9.Text = url;
}
Well you didn't set any url (no page is loaded inside the web browser). You could try this:
webBrowser1.Url = new Uri("http://www.google.com", UriKind.Absolute);
And get the url this way: webBrowser1.Url.ToString();
Wait for the page to load and the press then button.
I thought id comment on this, I literally took your
"webBrowser1.Url.AbsoluteUri;"
and in my case im using a combotextbox so Double click your browser form and it will take you to the even handler, i just put
"combobox1.text= webBrowser1.Url.AbsoluteUri;"
and it works for me now. You got me on the time, but whatever you need to check, check for the combobox1.text or whatever you are using for your url's
if your browser1 is chromiumwebbrowser, then use
string url = browser1.Address;
call the url and you will get it.

How to stop a event from firing + ASP.NET

I have a WebPage on which I have to detect and prevent usage of Browser Refresh Button. (Now Please, dont suggest me Response.Redirect, I wont be able to use it in this scenario).
On looking at this page http://aspalliance.com/687_Preventing_Duplicate_Record_Insertion_on_Page_Refresh.4 I found the way. I'm planning to put this idea in a Control and place the control on every page.
Now that my page contains so many buttons and other controls. My concern is, If it is a refresh post... I dont want any events to get fired...
It will be tedious to go and check whether it is a refresh post in the beginning of every event as my entire application is almost built.
Any ideas that would help me.
Raja
Including the below text for sake of more clarity :
Hi All, I hope a little misunderstanding... I dont want to stop user pressing the Refresh button... But all I want is to adjust my application's response accordingly... Imagine a scenario, when user clicks BUTTON-A a popup window opens with another page. Now when the user comes back and clicks refresh button in the main window, the click event of BUTTON-A is fired again and popup window is opened again... In such scenario, I want to refresh the page as such, without opening the popup window. so, I need to stop ASP.NET from firing the click event of BUTTON-A (or any other similar buttons)
I know you're not going to want to hear this but users expect to be able to hit the refresh button. Breaking something they like will make them unhappy. They'll blame you and your name will be mud.
Just think about those sites that try to block the Back button: do you like them?
This is at least a starting point on how you can do it. I'm not sure all logic is 100%, but it is something to begin with...
protected void Page_Load(object sender, EventArgs e)
{
foreach (Control control in Controls)
{
DisableEvent(control);
}
}
protected void Page_PreRender(object sender, EventArgs e)
{
foreach (Control control in Controls)
{
UpdateViewstate(control);
}
}
private void DisableEvent(Control current)
{
foreach (Control control in current.Controls)
{
if (control.GetType() == typeof(Button))
{
if (IsPostBack)
{
if (Session["update" + control.ID].ToString() != ViewState["update" + control.ID].ToString())
{
RemoveClickEvent((Button)control);
}
else
{
((Button)control).Click += new EventHandler(Button_Disable);
}
}
else
{
Session["update" + control.ID] = Server.UrlEncode(System.DateTime.Now.ToString());
}
}
DisableEvent(control);
}
}
private void UpdateViewstate(Control current)
{
foreach (Control control in current.Controls)
{
if (control.GetType() == typeof(Button))
{
ViewState["update" + control.ID] = Session["update" + control.ID];
}
UpdateViewstate(control);
}
}
void RemoveClickEvent(Button b) {
System.Reflection.FieldInfo f1 = typeof(Button).GetField("EventClick", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
object obj = f1.GetValue(b);
System.Reflection.PropertyInfo pi = typeof(Button).GetProperty("Events", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
System.ComponentModel.EventHandlerList list = (System.ComponentModel.EventHandlerList)pi.GetValue(b, null);
list.RemoveHandler(obj, list[obj]);
}
protected void Button_Disable(object sender, EventArgs e)
{
Button b = (Button)sender;
Session["update" + b.ID] = Server.UrlEncode(System.DateTime.Now.ToString());
}
You cannot prevent the user from pressing the Refresh button. It's build in functionality in the browser that resends the previous request, get or post. It's just there.
That is why that you normally make a redirect after a post to a get (e.g. the user posts to user/1/edit and the response redirects to user/1/view), so that a refresh will not cause double post.
I'm sorry that this is not what you want to hear, but when making a web application, you should try to follow web standards and let the user be able to use those browser features that he/she expects: back, forward, and refresh. And I know that your application is almost finished.
But if you start creating hacks for preventing refresh, or other stuff where you're not flowing with the technology, but going up against the stream, your application will start carrying around a bad package, and as the application lives on and is extended, this bad package is going to be a burden for further development.

Categories

Resources