0

I have a C# winform project with a webbrowser control. I'm loading an HTML page with images into the webbrowser. Each image has a different ID:

<img src="F:\Temp\file12948.jpg" id="12948" width="180px"> 

Is there a way to pass the ID into a variable when clicking on the image so I can use the ID in my code? The path to the image can also be used as I can extract the number from there.

I have already searched here there and everywhere for a solution but can't find anything related.

3
  • If you don't want any javascript code in the page, Possible duplicate of .Net How to get the ID of the clicked Element in a Webbrowser. If you are happy with jsavascript click handlers, Possible duplicate of WinForms - How do I execute C# application code from inside WebBrowser control?. Commented Sep 9, 2017 at 18:47
  • Thanks, already seen and tested the first one though. It seems to require using the webbrowser context menu rather than the left mouse button, which is what I'm looking for. Commented Sep 9, 2017 at 18:49
  • The JavaScript thing might do, if nothing else works. Are there any disadvantages to this other than the user may have disabled javascript in IE? Commented Sep 9, 2017 at 18:56

2 Answers 2

3

You can dynamically attach to image's onClick event.

public class TestForm : Form { WebBrowser _WebBrowser = null; public TestForm() { _WebBrowser = new WebBrowser(); _WebBrowser.ScriptErrorsSuppressed = true; _WebBrowser.Dock = DockStyle.Fill; this.Controls.Add(_WebBrowser); WebBrowserDocumentCompletedEventHandler Completed = null; Completed = (s, e) => { //add onclick event dynamically foreach (var img in _WebBrowser.Document.GetElementsByTagName("img").OfType<HtmlElement>()) { img.AttachEventHandler("onclick", (_, __) => OnClick(img)); } _WebBrowser.DocumentCompleted -= Completed; }; _WebBrowser.DocumentCompleted += Completed; var imgurl = "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png"; //_WebBrowser.Navigate("http://edition.cnn.com/2017/09/09/us/hurricane-irma-cuba-florida/index.html"); _WebBrowser.DocumentText = $"<html> <img src='{imgurl}' id=123 /> </html>"; } void OnClick(HtmlElement img) { MessageBox.Show(img.GetAttribute("id")); } } 
Sign up to request clarification or add additional context in comments.

1 Comment

Well this one does exactly what I want. Excellent, thanks!
1

On simple way would be to use browser navigation. When clicking you can navigate to a special URL, then you handle the Navigating event and if the url is the special url you cancel the navigation and handle the data.

public MainWindow() { InitializeComponent(); br.NavigateToString(@"<a href=""http://messages?id=12948""><img src=""F:\Temp\file12948.jpg"" id=""12948"" width=""180px"" ></a>"); br.Navigating += this.Br_Navigating; } private void Br_Navigating(object sender, NavigatingCancelEventArgs e) { if(e.Uri.Host == "messages") { MessageBox.Show(e.Uri.Query); e.Cancel = true; } } 

This works if you have some control over the HTML. You could also set the URL from JS if you don't want to add the anchor.

Edit

The above version is for a WPF application. The winforms version is as follows:

public Form1() { InitializeComponent(); webBrowser1.DocumentText = @"<a href=""http://messages?id=12948""><img src=""F:\Temp\file12948.jpg"" id=""12948"" width=""180px"" ></a>"; webBrowser1.Navigating += this.webBrowser1_Navigating; } private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e) { if (e.Url.Host == "messages") { MessageBox.Show(e.Url.Query); e.Cancel = true; } } 

15 Comments

Well it's part of a database project, all data are local. I've been thinking of something similar - navigate to another webbrowser instance instead of opening IE (I have the code for that) and then get the Uri from there.
It was my understanding you were using the web browser control, if so, the navigation would happen in the same instance, and if you cancel the navigation the users will never notice any navigation
Well this code seems to be for WPF (does work there), but NavigatingCancelEventArgs is not supported by WinForms it seems.
@Rado Creating a win forms version now will post shortly, should still work.
@Rado added a winforms version, the event arg type was different, but the technique works in pretty much the same way.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.