Windows Develop Bookmark and Share   
 index > Windows Forms General > Need to retrieve and set a variable equal to a return value from ASP page using a WebBrowser control...
 

Need to retrieve and set a variable equal to a return value from ASP page using a WebBrowser control...

This is a somewhat complicated question for me to explain but I am going to do my best. I am currently in the process of rewriting an intranet based web application that was used to launch various pages and systems. Since it is going to be a phased implementation the first phase was to replace the basic entry point to all pages which I have successfully done, bar 2 entries that I am having trouble handling. What makes them so difficult is that they are really 2 step entries...

Essentially the first step is to enter a modal popup page where an order is entered. After the order is entered the modal popup's response text is the orderId which is then used to navigate away from the home page (entry to systems page) to the order details page. The code from the homepage currently looks like this:

function orderModal(url,altUrl)
{
     var intOrderID = window.showModalDialog('/OrderEntry/'+url+'?Editing=0','','dialogWidth:1022px;dialogHeight:600px;scroll:yes;status:no;help:no;center:yes');
     
     if(parseInt(intOrderID))
     {
          window.open('/OrderEntry/'+altUrl+'?OrderID=' + intOrderID + '&NewMode=1','_self');
     }
}
Basically what I have opted to do is create a form that uses the webbrowser component and navigates to the url. However when they click save on that page I need to intercept the javascript execution or webresponse text and parse out the intOrderId variable so that I can then navigate the webbrowser control to the 2nd altUrl.

Does anyone have any suggestions or know of a way of doing this? I would assume that it is possible because I have written code to call javascript functions within a webbrowser.document before. I have just never pulled a value from a webbrowser.document after execution has been performed.

Thanks in advance,
Matt Colin
KarmaKid15  Wednesday, September 30, 2009 4:28 PM
Well, I answered my own question on this one... for anyone that would like to know how it's possible I did the following.

I set up a method on my winform taking an orderId as a parameter and marked the winform as COMVisible. After doing so I added code to the existing asp page where the return value was being set and before closing the modal page I used the window.external.<methodName> in this case <methodName> = LaunchDetails(orderId) method that I had created on the winforms page and viola. Worked perfectly. To maintain usability with the legacy system I wrapped the external call in a try catch and simple didn't handle the error that is received when no external application is visible.

Code:

try
{
     window.external.LaunchDetails(window.returnValue);
}
catch(e)
{
     //do nothing...
}

The above is the javascript implementation and below is the attribute added to the form along with the method to launch the other page.

Imports System.Runtime.InteropServices

<ComVisible(True)> _
Public Class WebModal

    Public Sub LaunchDetails(ByVal orderId As Integer)
        Dim deleg As New Action(Of Integer)(AddressOf LaunchDetailsAsync)
        deleg.BeginInvoke(orderId, AddressOf DirectCast(Me.Tag, Desktop).ApplicationLaunched, Nothing)
    End Sub

    Private Sub LaunchDetailsAsync(ByVal orderId As Integer)
        Process.Start(Me.AltAddress.ToString & "?OrderID=" & orderId & "&NewMode=1")
    End Sub

EndClass
That's really all there is too it. Pretty simple stuff actually. Hope this helps anyone who has similar problems.
  • Marked As Answer byKarmaKid15 Wednesday, September 30, 2009 7:59 PM
  •  
KarmaKid15  Wednesday, September 30, 2009 7:59 PM
Well, I answered my own question on this one... for anyone that would like to know how it's possible I did the following.

I set up a method on my winform taking an orderId as a parameter and marked the winform as COMVisible. After doing so I added code to the existing asp page where the return value was being set and before closing the modal page I used the window.external.<methodName> in this case <methodName> = LaunchDetails(orderId) method that I had created on the winforms page and viola. Worked perfectly. To maintain usability with the legacy system I wrapped the external call in a try catch and simple didn't handle the error that is received when no external application is visible.

Code:

try
{
     window.external.LaunchDetails(window.returnValue);
}
catch(e)
{
     //do nothing...
}

The above is the javascript implementation and below is the attribute added to the form along with the method to launch the other page.

Imports System.Runtime.InteropServices

<ComVisible(True)> _
Public Class WebModal

    Public Sub LaunchDetails(ByVal orderId As Integer)
        Dim deleg As New Action(Of Integer)(AddressOf LaunchDetailsAsync)
        deleg.BeginInvoke(orderId, AddressOf DirectCast(Me.Tag, Desktop).ApplicationLaunched, Nothing)
    End Sub

    Private Sub LaunchDetailsAsync(ByVal orderId As Integer)
        Process.Start(Me.AltAddress.ToString & "?OrderID=" & orderId & "&NewMode=1")
    End Sub

EndClass
That's really all there is too it. Pretty simple stuff actually. Hope this helps anyone who has similar problems.
  • Marked As Answer byKarmaKid15 Wednesday, September 30, 2009 7:59 PM
  •  
KarmaKid15  Wednesday, September 30, 2009 7:59 PM

You can use google to search for other answers

Custom Search

More Threads

• Detecting if a font family has a bold font
• Security Update for Microsoft .NET Framework, Version 2.0 (KB928365) and localized forms problem
• recarding form
• inherited user control
• how can I lose the caret in the Combobox at runtime.
• MDI Enable/Disable Parent MenuItem
• Application.Exit()
• Rogue browser control - AxSHDocVw in Windows Forms App
• Windows form application blinking
• finding out function name