AProgrammer
AProgrammer

Reputation: 158

WPF Off-Screen WebBrowser Control - Javascript on pageload not working

I am trying to navigate to an aspx using WebBrowser control in wpf. The page has a javascript on onload. Javascript call will work if i place control on a grid an make the visibility of control to collapsed or hidden but I want only off-screen call. Is there a way to do that or something impossible?

aspx:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function OnloadJSCall() {
           window.PageMethods.Docall(onSuccess, onFailure);
        }

        function onSuccess(result) {
           
        }


        function onFailure(error) {
           
        }

</script>
</head>
<body>
    <form id="form1" runat="server">
    <div><asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
    <script language="JavaScript"> OnloadJSCall();</script>
    </div>
    </form>
</body>
</html>

public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [System.Web.Services.WebMethod]
        public static string Docall()
        {
            using (var fs = new FileStream("C:\\AD\\Test1111.txt", FileMode.Append, FileAccess.Write))
            using (var sw = new StreamWriter(fs))
            {
                sw.WriteLine(System.DateTime.Now);
            }

            return "Done";
        }
    }

WPF App

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        
    </Grid>
</Window>

MainWindow.xaml.cs

  public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            var webBrowserControl = new WebBrowser();
            webBrowserControl.Navigate("http://localhost:53288/WebForm1.aspx");
            webBrowserControl.Navigated += webBrowserControlOnNavigated;
        }

        private void webBrowserControlOnNavigated(object sender, NavigationEventArgs navigationEventArgs)
        {
        }
    }

Upvotes: 1

Views: 629

Answers (1)

AProgrammer
AProgrammer

Reputation: 158

Ok I got the answer. If no ui component then Javascript won't work. So added the control to a form and it worked!!

        var newForm =new Form();

        var webBrowserControl = new System.Windows.Forms.WebBrowser();
        webBrowserControl.Navigate("http://localhost:53288/WebForm1.aspx");

        newForm.Controls.Add(webBrowserControl);

Upvotes: 1

Related Questions