1

My goal is to use an WPF app to add some field validation and logic to a legacy web form that my users have to interact with.

I've build the form fields and logic in WPF, and can open a browser page using cefsharp. Now I'm trying to figure out how to fill in the fields, and call some JavaScript functions.

So, for example I'm trying to open a frame by firing the below javascript after the page loads:

<a tabindex="0" href="javascript:;" onclick="showTicket();" onmouseover="window.status='Insert a new ticket';return true;" onmouseout="window.status=''">Ticket</a>

private void Ticket_Click(object sender, RoutedEventArgs e)
{
    BrowserControl.ExecuteScriptAsync("showTicket();");
}

But that doesn't actually render the frame, and I've tried calling the mainframe, but that didn't work either, do I need to find the html element and call the onclick button event instead, or maybe find the frame, but I think that should be handled by the script with I do not have access to.

The next thing that I need to do once the frame is open, is to fill in some html fields, like so:

<textarea class="form-control required" maxlength="255" name="Description"  tabindex="0" id="html001HTML" rows="3"></textarea>

The users will have to interact further with the form, so that it is why I am not going to handle everything programmatically. I just need to add a bunch of pre selected text to text fields.

Zen
  • 7,197
  • 8
  • 35
  • 57
Mark Garcia
  • 221
  • 1
  • 2
  • 15
  • 1
    Why don't you're calling the web methods wich are using by the legacy web application? – Jesus Angulo Apr 05 '16 at 17:43
  • So I should call the Iframe instead? [code] Can you provide and example of how I would do that? – Mark Garcia Apr 05 '16 at 18:06
  • Firstly if you haven't already read https://github.com/cefsharp/CefSharp/wiki/Frequently-asked-questions#1-how-do-you-call-a-javascript-methods-from-net Secondly, try opening `DevTools` to see what's going on, make sure you can manually execute your methods. When executing script, I usually recommend using an `Anonymous Closure`. – amaitland Apr 05 '16 at 21:26
  • Thank you for your help. – Mark Garcia Apr 07 '16 at 03:16

1 Answers1

1

After hours of searching, and working with Chrome's Devtools I finally found the answer: First I had to find the iframe, and then use contentWindow to call the function.

BrowserControl.ExecuteScriptAsync("document.getElementById('iframe1').contentWindow.Ticket()");

I found the answer here: Invoking JavaScript code in an iframe from the parent page

Community
  • 1
  • 1
Mark Garcia
  • 221
  • 1
  • 2
  • 15